Flag control variable

L

luke.geelen

hello,
i'd like to know how to set up a flag to change a variable,
for example, i want a simple script to combine 2 numbers,


sum = num + another_num
print "Now the sum of the numbers equals : ", sum

how could i make it so that if i type python ./script.py 21 41
that i get the sum of 21 and 41 ?

luke
 
L

Larry Martell

hello,
i'd like to know how to set up a flag to change a variable,
for example, i want a simple script to combine 2 numbers,


sum = num + another_num
print "Now the sum of the numbers equals : ", sum

how could i make it so that if i type python ./script.py 21 41
that i get the sum of 21 and 41 ?

Google for python command line arguments.
 
P

Peter Otten

i'd like to know how to set up a flag to change a variable,
for example, i want a simple script to combine 2 numbers,


sum = num + another_num
print "Now the sum of the numbers equals : ", sum

how could i make it so that if i type python ./script.py 21 41
that i get the sum of 21 and 41 ?

You seem to be looking for sys.argv which contains the script name and the
command-line arguments.

$ cat script.py
import sys
a = int(sys.argv[1])
b = int(sys.argv[2])
print a, "+", b, "=", a + b
$ python script.py 21 41
21 + 41 = 62

The conversion to int (or float etc.) is necessary because in python
'2141'
 
L

luke.geelen

when expandig the script to multiple calcs i got a problem
File "<stdin>", line 1
sign = *
^
SyntaxError: invalid syntax

is there a way of adding * without quoting marks, because if you do it just soms the arguments
 
T

Tim Chase

when expandig the script to multiple calcs i got a problem

File "<stdin>", line 1
sign = *
^
SyntaxError: invalid syntax

is there a way of adding * without quoting marks, because if you do
it just soms the arguments --

You want to store the actual operation. The "operator" module makes
this fairly easy, so you can do something like

import operator as o
operations = {
"*": o.mul,
"+": o.add,
"/": o.div,
"-": o.sub,
}

a = 32
c = 51
operation = operations["*"]
print(operation(a,c))

-tkc
 
L

luke.geelen

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 +
 
T

Tim Chase

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 +

This is the fault of your shell (bash perhaps)?

Try this:

bash$ echo +
+
bash$ echo *
(a list of files in your current directory here)

which occurs because of file-globbing.

You have a couple options that occur to me:

1) quote the asterisk:

bash$ ./mycode.py 3 "*" 2

which will let Python see it without the shell expanding it

2) use a different character/string such as "3 times 2"

3) pass the whole thing as a quoted string and then let Python do the
splitting:

bash$ ./mycode.py "3 * 2"

a, operator, b = argv[1:].split()
print(a,b,c)

-tkc
 
P

Peter Otten

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 +
 
G

Gary Herron

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
 
L

luke.geelen

Op dinsdag 11 februari 2014 19:55:59 UTC+1 schreef Gary Herron:
well i'm trying something else but no luck :


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
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

how do you meen "what does your command line look like?"
 
L

luke.geelen

Op dinsdag 11 februari 2014 19:51:40 UTC+1 schreef Peter Otten:
(e-mail address removed) wrote:


well i'm trying something else but no luck :




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

% (a, b, sum) os.system (command1)

elif sign == "*":
sum = a * b
print a, sign, b, "=", a * b
command1 = "sudo mpg321

% (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:


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: '*'
 
L

luke.geelen

Op dinsdag 11 februari 2014 20:01:05 UTC+1 schreef (e-mail address removed):
Op dinsdag 11 februari 2014 19:51:40 UTC+1 schreef Peter Otten:
(e-mail address removed) wrote:
well i'm trying something else but no luck :
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
% (a, b, sum) os.system (command1)
elif sign == "*":
sum = a * b
print a, sign, b, "=", a * b
command1 = "sudo mpg321
% (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:
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 ?
 
G

Gary Herron

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
how do you meen "what does your command line look like?"

When you run this python script, *how* do you do so?

Perhaps you type something like:
python script.py 21 '*' 42
If not, then how do you supply values for the script's sys.argv?

If it is like that, then I see the most likely potential problem. The
asterisk character (on Linux at least) is considered a wild-card
character -- it is replaced by a list of local files so your command becomes
python script.py 21 somefile1 somefile2 somefile3 <...and so on.> 42

If you put it in quotes, then it won't be expanded (at least in the
usual Linux shells -- you system may vary) and you'll end up with the
asterisk in sys.argv[2] and the number in sys.argv[3].

Gary Herron
 
J

Jussi Piitulainen

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: '*'

You've mis-spelt sigh.

This is not the code that you posted.

You misunderestimate that error message. It tells everything.
 
G

Gary Herron

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: '*'

Stop trying to guess what is going on. Print out sys.argv, and *see*
what values are there. Then read the error message.

You wrote your script expecting sys.argv[2] to contain an int, but in
fact (according to the error) it contains a '*' -- which can't be
converted to an integer obviously. Your error is in running the script
incorrectly, *OR* in your understanding of how the command line
arguments get placed in sys.argv. In either case you best bet is to
examine sys.argv by printing it (or examining it within a debugger) and
*see* what values it contains. Then adjust your script (or the running
of it) accordingly.

These are very beginner level debugging suggestions. If you develop the
skill to read and understand the error messages, and the skill to print
(or otherwise examine) the values your program is dealing with, you
progress will by 100's of times faster then this slow wait for someone
to respond to on this list.

Gary Herron
 
G

Gary Herron

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 ?

That's not really a Python question. The shell (as it's called) which
interprets your typed command and runs Python with the rest of the
command line arguments is in control of this. If you can find a way to
tell your shell to not expand '*' characters, or find a shell that does
not do so, then yes, you can dispense with the back-slash.

Gary Herron
 
T

Tim Chase

1) PLEASE either stop using Google Groups or take the time to remove
the superfluous white-space you keep adding to your posts/replies

2) you shouldn't need to use "sudo" to play sounds. That's just a
bad practice waiting for trouble.

-tkc
 
L

luke.geelen

Op dinsdag 11 februari 2014 20:28:44 UTC+1 schreef Tim Chase:
1) PLEASE either stop using Google Groups or take the time to remove

the superfluous white-space you keep adding to your posts/replies



2) you shouldn't need to use "sudo" to play sounds. That's just a

bad practice waiting for trouble.



-tkc

its one rule in the original (at least on my computer)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top