Flag control variable
Peter Otten
__peter__ at web.de
Tue Feb 11 13:51:40 EST 2014
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 +
More information about the Python-list
mailing list