Flag control variable

Gary Herron gary.herron at islandtraining.com
Tue Feb 11 13:55:59 EST 2014


On 02/11/2014 10:37 AM, luke.geelen at gmail.com wrote:
> well i'm trying something else but no luck :
>
> #!bin/bash/python
> import sys
> import os
> 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 +

Look at the error message.  Carefully!  It says, quite clearly, the call 
to int is being passed a string "Adafruit-Raspberry-Pi-Python-Code", 
which of course can't be converted to an integer.

Now the question is how you ran the program in such a manner that 
sys.argv[3] has such an odd value.
What does your command line look like?  You didn't tell us, but that's 
where the trouble is.

Gary Herron



More information about the Python-list mailing list