Flag control variable

luke.geelen at gmail.com luke.geelen at gmail.com
Tue Feb 11 14:06:42 EST 2014


Op dinsdag 11 februari 2014 20:01:05 UTC+1 schreef luke.... at gmail.com:
> Op dinsdag 11 februari 2014 19:51:40 UTC+1 schreef Peter Otten:
> 
> > luke.geelen at gmail.com wrote:
> 
> > 
> 
> > 
> 
> > 
> 
> > > well i'm trying something else but no luck :
> 
> > 
> 
> > > 
> 
> > 
> 
> > > #!bin/bash/python
> 
> > 
> 
> > 
> 
> > 
> 
> > Hm.
> 
> > 
> 
> > 
> 
> > 
> 
> > > import sys
> 
> > 
> 
> > > import os
> 
> > 
> 
> > 
> 
> > 
> 
> > For debugging purposes put the line
> 
> > 
> 
> > 
> 
> > 
> 
> > print sys.argv
> 
> > 
> 
> > 
> 
> > 
> 
> > here to see what arguments are passed to the script. When you type
> 
> > 
> 
> > 
> 
> > 
> 
> > $ python script.py 2 * 2
> 
> > 
> 
> > 
> 
> > 
> 
> > in the shell the "*" sign is replaced with all items in the current 
> 
> > 
> 
> > directory. To avoid that you have to escape, i. e. prepend a backslash:
> 
> > 
> 
> > 
> 
> > 
> 
> > $ python script.py 2 \* 2
> 
> > 
> 
> > 
> 
> > 
> 
> > To illustrate:
> 
> > 
> 
> > 
> 
> > 
> 
> > $ touch one two three
> 
> > 
> 
> > $ ls
> 
> > 
> 
> > one  three  two
> 
> > 
> 
> > $ python -c 'import sys; print sys.argv' 2 + 2
> 
> > 
> 
> > ['-c', '2', '+', '2']
> 
> > 
> 
> > $ python -c 'import sys; print sys.argv' 2 * 2
> 
> > 
> 
> > ['-c', '2', 'one', 'three', 'two', '2']
> 
> > 
> 
> > $ python -c 'import sys; print sys.argv' 2 \* 2
> 
> > 
> 
> > ['-c', '2', '*', '2']
> 
> > 
> 
> > 
> 
> > 
> 
> > > a = int(sys.argv[1])
> 
> > 
> 
> > > sign = (sys.argv[2])
> 
> > 
> 
> > > b = int(sys.argv[3])
> 
> > 
> 
> > > 
> 
> > 
> 
> > > if sign == '+':
> 
> > 
> 
> > >   sum = a + b
> 
> > 
> 
> > >   print a, sign, b, "=", a + b
> 
> > 
> 
> > >   command1 = "sudo mpg321 
> 
> > 
> 
> > >   'http://translate.google.com/translate_tts?tl=en&q=%s_plus%s_equals%s'"
> 
> > 
> 
> > >   % (a, b, sum) os.system (command1)
> 
> > 
> 
> > > 
> 
> > 
> 
> > > elif sign == "*":
> 
> > 
> 
> > >   sum = a * b
> 
> > 
> 
> > >   print a, sign, b, "=", a * b
> 
> > 
> 
> > >   command1 = "sudo mpg321 
> 
> > 
> 
> > >   'http://translate.google.com/translate_tts?tl=en&q=%s_times%s_equals%s'"
> 
> > 
> 
> > >   % (a, b, sum)
> 
> > 
> 
> > > 
> 
> > 
> 
> > > when using * i get
> 
> > 
> 
> > > 
> 
> > 
> 
> > > Traceback (most recent call last):
> 
> > 
> 
> > >   File "./math+.py", line 6, in <module>
> 
> > 
> 
> > >     b = int(sys.argv[3])
> 
> > 
> 
> > > ValueError: invalid literal for int() with base 10:
> 
> > 
> 
> > > 'Adafruit-Raspberry-Pi-Python-Code'
> 
> > 
> 
> > > 
> 
> > 
> 
> > > i don't understand why b is a problem, it works fine with +
> 
> 
> 
> when using python script.py 2 \* 2
> 
> i get 
> 
> 
> 
> Traceback (most recent call last):
> 
>   File "math2.py", line 5, in <module>
> 
>     sign = int(sys.argv[2])
> 
> ValueError: invalid literal for int() with base 10: '*'

i found it int(sys.argv[2]) should be sys.argv[2]

is there a way i can do python ./script.py 3 * 3 instead of python ./script 3 \* 3 ?



More information about the Python-list mailing list