Re: Flag control variable
Op woensdag 12 februari 2014 06:23:14 UTC+1 schreef Dave Angel: luke.gee...@gmail.com Wrote in message: Can I make it that if C = int(sys.argv[3]) But when I only enter 2 argumentvariable it sets c automaticly to 0 or 1 Why do you ask for 'automatically'? You're the programmer, write the test in the code. if len (sys.argv) == 3: sys.argv. append (0) But of course there are lots of other things you need to check, so consider all of them at the same time. -- DaveA then i keep getting IndexError: list index out of range anyway to prevent it and just set the value to 0? -- https://mail.python.org/mailman/listinfo/python-list
Re: Flag control variable
On 12/02/2014 15:32, luke.gee...@gmail.com wrote: Op woensdag 12 februari 2014 06:23:14 UTC+1 schreef Dave Angel: luke.gee...@gmail.com Wrote in message: Can I make it that if C = int(sys.argv[3]) But when I only enter 2 argumentvariable it sets c automaticly to 0 or 1 Why do you ask for 'automatically'? You're the programmer, write the test in the code. if len (sys.argv) == 3: sys.argv. append (0) But of course there are lots of other things you need to check, so consider all of them at the same time. -- DaveA then i keep getting IndexError: list index out of range anyway to prevent it and just set the value to 0? Please find a semi-decent tool to use or always follow the instructions for how to remove the double line spacing when using gg, thanks. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com -- https://mail.python.org/mailman/listinfo/python-list
Re: Flag control variable
luke.gee...@gmail.com writes: Can I make it that if C = int(sys.argv[3]) But when I only enter 2 argumentvariable it sets c automaticly to 0 or 1 C = int(sys.argv[3]) if len(sys.argv) 3 else 0 is one possibility. -- Alain. -- https://mail.python.org/mailman/listinfo/python-list
Re: Flag control variable
Op woensdag 12 februari 2014 17:10:36 UTC+1 schreef Alain Ketterlin: luke.gee...@gmail.com writes: Can I make it that if C = int(sys.argv[3]) But when I only enter 2 argumentvariable it sets c automaticly to 0 or 1 C = int(sys.argv[3]) if len(sys.argv) 3 else 0 is one possibility. -- Alain. thanks a lot -- https://mail.python.org/mailman/listinfo/python-list
Re: Flag control variable
luke.gee...@gmail.com Wrote in message: Deleting all the obnoxious doublespaced googlegroups nonsense. .. then i keep getting IndexError: list index out of range anyway to prevent it and just set the value to 0? My car makes a funny noise. What kind of coat should I wear to the dance to fix it? And do I turn left or back up at the corner? -- DaveA -- https://mail.python.org/mailman/listinfo/python-list
Re: Flag control variable
On Tuesday, February 11, 2014, luke.gee...@gmail.com wrote: 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. -- https://mail.python.org/mailman/listinfo/python-list
Re: Flag control variable
luke.gee...@gmail.com wrote: 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 21 + 41 '2141' -- https://mail.python.org/mailman/listinfo/python-list
Re: Flag control variable
Thanks a lot -- https://mail.python.org/mailman/listinfo/python-list
Re: Flag control variable
when expandig the script to multiple calcs i got a problem a = 32 c = 51 sign = * 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 -- https://mail.python.org/mailman/listinfo/python-list
Re: Flag control variable
On 2014-02-11 10:16, luke.gee...@gmail.com wrote: when expandig the script to multiple calcs i got a problem a = 32 c = 51 sign = * 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 -- https://mail.python.org/mailman/listinfo/python-list
Re: Flag control variable
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=enq=%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=enq=%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 + -- https://mail.python.org/mailman/listinfo/python-list
Re: Flag control variable
On 2014-02-11 10:37, luke.gee...@gmail.com wrote: command1 = sudo mpg321 'http://translate.google.com/translate_tts?tl=enq=%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 -- https://mail.python.org/mailman/listinfo/python-list
Re: Flag control variable
luke.gee...@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=enq=%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=enq=%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 + -- https://mail.python.org/mailman/listinfo/python-list
Re: Flag control variable
Op dinsdag 11 februari 2014 19:55:59 UTC+1 schreef Gary Herron: On 02/11/2014 10:37 AM, luke.gee...@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=enq=%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=enq=%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? -- https://mail.python.org/mailman/listinfo/python-list
Re: Flag control variable
On 02/11/2014 10:37 AM, luke.gee...@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=enq=%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=enq=%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 -- https://mail.python.org/mailman/listinfo/python-list
Re: Flag control variable
Op dinsdag 11 februari 2014 19:51:40 UTC+1 schreef Peter Otten: luke.gee...@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=enq=%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=enq=%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: '*' -- https://mail.python.org/mailman/listinfo/python-list
Re: Flag control variable
On 02/11/2014 10:59 AM, luke.gee...@gmail.com wrote: 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 -- https://mail.python.org/mailman/listinfo/python-list
Re: Flag control variable
On 11/02/2014 18:59, luke.gee...@gmail.com wrote: Would you please read and action this https://wiki.python.org/moin/GoogleGroupsPython to prevent us seeing the double line spaced text that I've snipped, thanks. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com -- https://mail.python.org/mailman/listinfo/python-list
Re: Flag control variable
Op dinsdag 11 februari 2014 20:01:05 UTC+1 schreef luke@gmail.com: Op dinsdag 11 februari 2014 19:51:40 UTC+1 schreef Peter Otten: luke.gee...@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=enq=%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=enq=%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 ? -- https://mail.python.org/mailman/listinfo/python-list
Re: Flag control variable
luke.gee...@gmail.com writes: 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. -- https://mail.python.org/mailman/listinfo/python-list
Re: Flag control variable
On 02/11/2014 11:01 AM, luke.gee...@gmail.com wrote: 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 -- https://mail.python.org/mailman/listinfo/python-list
Re: Flag control variable
On 02/11/2014 11:06 AM, luke.gee...@gmail.com wrote: 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 -- https://mail.python.org/mailman/listinfo/python-list
Re: Flag control variable
On 2014-02-11 11:06, luke.gee...@gmail.com wrote: command1 = sudo mpg321 'http://translate.google.com/translate_tts?tl=enq=%s_times%s_equals%s' 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 -- https://mail.python.org/mailman/listinfo/python-list
Re: Flag control variable
Op dinsdag 11 februari 2014 20:28:44 UTC+1 schreef Tim Chase: On 2014-02-11 11:06, luke.gee...@gmail.com wrote: command1 = sudo mpg321 'http://translate.google.com/translate_tts?tl=enq=%s_times%s_equals%s' 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) -- https://mail.python.org/mailman/listinfo/python-list
Re: Flag control variable
hey, i got another problem now, if i use the imterpreter to do 3 * 4 it gives twelve the script gives ? any tips -- https://mail.python.org/mailman/listinfo/python-list
Re: Flag control variable
On 11/02/2014 19:54, luke.gee...@gmail.com wrote: 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 For the THIRD time, would you please read and action this https://wiki.python.org/moin/GoogleGroupsPython to prevent us seeing the double line spacing which I've AGAIN snipped, thanks. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com -- https://mail.python.org/mailman/listinfo/python-list
Re: Flag control variable
On 02/11/2014 11:55 AM, luke.gee...@gmail.com wrote: hey, i got another problem now, if i use the imterpreter to do 3 * 4 it gives twelve the script gives ? any tips 3*4 12 3*4 '' Multiplying two integers produces the result you expect. Multiplying a *string* by an integer is what you are doing. (And it just repeats the string a number of times -- not what you want.) Your code used to have int(...) to convert the string supplied by sys.argv into integers. What happened to them? Gary Herron -- https://mail.python.org/mailman/listinfo/python-list
Re: Flag control variable
Would it be possible to make an int(sys.argv[1]) Not needed and set value 0 ( or in another script 1) For example a = int(sys.argv[1]) b = int(sys.argv[2]) c = int(sys.argv[3]) And I run Python ./script.py 2 3 It just set c automaticly to 0 or 1 Luke (PS thanks for the quick help) -- https://mail.python.org/mailman/listinfo/python-list
Re: Flag control variable
On 02/11/2014 01:18 PM, luke.gee...@gmail.com wrote: Would it be possible to make an int(sys.argv[1]) Not needed and set value 0 ( or in another script 1) For example a = int(sys.argv[1]) b = int(sys.argv[2]) c = int(sys.argv[3]) And I run Python ./script.py 2 3 It just set c automaticly to 0 or 1 Luke (PS thanks for the quick help) That question does not make sense to me. Yes, you can set c=1 in your program, or zero if that's what you want, but this can't be the question you are really trying to ask. -- https://mail.python.org/mailman/listinfo/python-list
Re: Flag control variable
Can I make it that if C = int(sys.argv[3]) But when I only enter 2 argumentvariable it sets c automaticly to 0 or 1 -- https://mail.python.org/mailman/listinfo/python-list
Re: Flag control variable
luke.gee...@gmail.com Wrote in message: Can I make it that if C = int(sys.argv[3]) But when I only enter 2 argumentvariable it sets c automaticly to 0 or 1 Why do you ask for 'automatically'? You're the programmer, write the test in the code. if len (sys.argv) == 3: sys.argv. append (0) But of course there are lots of other things you need to check, so consider all of them at the same time. -- DaveA -- https://mail.python.org/mailman/listinfo/python-list