Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #65931 > unrolled thread

Flag control variable

Started byluke.geelen@gmail.com
First post2014-02-11 09:29 -0800
Last post2014-02-12 20:46 -0500
Articles 20 on this page of 33 — 10 participants

Back to article view | Back to comp.lang.python


Contents

  Flag control variable luke.geelen@gmail.com - 2014-02-11 09:29 -0800
    Re: Flag control variable Larry Martell <larry.martell@gmail.com> - 2014-02-11 12:44 -0500
    Re: Flag control variable Peter Otten <__peter__@web.de> - 2014-02-11 18:46 +0100
    Re: Flag control variable luke.geelen@gmail.com - 2014-02-11 10:00 -0800
    Re: Flag control variable luke.geelen@gmail.com - 2014-02-11 10:16 -0800
      Re: Flag control variable Tim Chase <python.list@tim.thechases.com> - 2014-02-11 12:32 -0600
    Re: Flag control variable luke.geelen@gmail.com - 2014-02-11 10:37 -0800
      Re: Flag control variable Tim Chase <python.list@tim.thechases.com> - 2014-02-11 12:51 -0600
      Re: Flag control variable Peter Otten <__peter__@web.de> - 2014-02-11 19:51 +0100
        Re: Flag control variable luke.geelen@gmail.com - 2014-02-11 11:01 -0800
          Re: Flag control variable luke.geelen@gmail.com - 2014-02-11 11:06 -0800
            Re: Flag control variable Gary Herron <gary.herron@islandtraining.com> - 2014-02-11 11:26 -0800
            Re: Flag control variable Tim Chase <python.list@tim.thechases.com> - 2014-02-11 13:28 -0600
              Re: Flag control variable luke.geelen@gmail.com - 2014-02-11 11:54 -0800
                Re: Flag control variable Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-02-11 20:02 +0000
          Re: Flag control variable Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2014-02-11 21:14 +0200
          Re: Flag control variable Gary Herron <gary.herron@islandtraining.com> - 2014-02-11 11:20 -0800
      Re: Flag control variable Gary Herron <gary.herron@islandtraining.com> - 2014-02-11 10:55 -0800
        Re: Flag control variable luke.geelen@gmail.com - 2014-02-11 10:59 -0800
          Re: Flag control variable Gary Herron <gary.herron@islandtraining.com> - 2014-02-11 11:09 -0800
          Re: Flag control variable Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-02-11 19:08 +0000
    Re: Flag control variable luke.geelen@gmail.com - 2014-02-11 11:55 -0800
      Re: Flag control variable Gary Herron <gary.herron@islandtraining.com> - 2014-02-11 12:19 -0800
        Re: Flag control variable luke.geelen@gmail.com - 2014-02-11 13:18 -0800
          Re: Flag control variable Gary Herron <gary.herron@islandtraining.com> - 2014-02-11 15:47 -0800
            Re: Flag control variable luke.geelen@gmail.com - 2014-02-11 21:09 -0800
              Re: Flag control variable Dave Angel <davea@davea.name> - 2014-02-12 00:23 -0500
                Re: Flag control variable luke.geelen@gmail.com - 2014-02-12 07:32 -0800
                  Re: Flag control variable Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-02-12 15:42 +0000
                  Re: Flag control variable Dave Angel <davea@davea.name> - 2014-02-12 13:51 -0500
              Re: Flag control variable Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2014-02-12 17:10 +0100
                Re: Flag control variable luke.geelen@gmail.com - 2014-02-12 09:15 -0800
              Re: Flag control variable Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-02-12 20:46 -0500

Page 1 of 2  [1] 2  Next page →


#65931 — Flag control variable

Fromluke.geelen@gmail.com
Date2014-02-11 09:29 -0800
SubjectFlag control variable
Message-ID<baffb663-4f6d-4580-ac5d-d99b4aaf3c40@googlegroups.com>
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

[toc] | [next] | [standalone]


#65932

FromLarry Martell <larry.martell@gmail.com>
Date2014-02-11 12:44 -0500
Message-ID<mailman.6686.1392140651.18130.python-list@python.org>
In reply to#65931

[Multipart message — attachments visible in raw view] — view raw

On Tuesday, February 11, 2014, <luke.geelen@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.

[toc] | [prev] | [next] | [standalone]


#65933

FromPeter Otten <__peter__@web.de>
Date2014-02-11 18:46 +0100
Message-ID<mailman.6687.1392140807.18130.python-list@python.org>
In reply to#65931
luke.geelen@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'

[toc] | [prev] | [next] | [standalone]


#65935

Fromluke.geelen@gmail.com
Date2014-02-11 10:00 -0800
Message-ID<4f701123-2d03-4ed4-8ef0-05e9e273e196@googlegroups.com>
In reply to#65931

Thanks a lot

[toc] | [prev] | [next] | [standalone]


#65937

Fromluke.geelen@gmail.com
Date2014-02-11 10:16 -0800
Message-ID<0938f7a5-702f-48f7-a8ef-cd0a2fb198df@googlegroups.com>
In reply to#65931
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

[toc] | [prev] | [next] | [standalone]


#65939

FromTim Chase <python.list@tim.thechases.com>
Date2014-02-11 12:32 -0600
Message-ID<mailman.6691.1392143537.18130.python-list@python.org>
In reply to#65937
On 2014-02-11 10:16, luke.geelen@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

[toc] | [prev] | [next] | [standalone]


#65940

Fromluke.geelen@gmail.com
Date2014-02-11 10:37 -0800
Message-ID<5019eb5d-ebda-4417-ab2d-9a58afcdd186@googlegroups.com>
In reply to#65931
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 +

[toc] | [prev] | [next] | [standalone]


#65941

FromTim Chase <python.list@tim.thechases.com>
Date2014-02-11 12:51 -0600
Message-ID<mailman.6692.1392144636.18130.python-list@python.org>
In reply to#65940
On 2014-02-11 10:37, luke.geelen@gmail.com wrote:
>   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


[toc] | [prev] | [next] | [standalone]


#65942

FromPeter Otten <__peter__@web.de>
Date2014-02-11 19:51 +0100
Message-ID<mailman.6693.1392144693.18130.python-list@python.org>
In reply to#65940
luke.geelen@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 +

[toc] | [prev] | [next] | [standalone]


#65947

Fromluke.geelen@gmail.com
Date2014-02-11 11:01 -0800
Message-ID<cffe3469-a01c-45b1-9407-72de17e7958c@googlegroups.com>
In reply to#65942
Op dinsdag 11 februari 2014 19:51:40 UTC+1 schreef Peter Otten:
> luke.geelen@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: '*'

[toc] | [prev] | [next] | [standalone]


#65949

Fromluke.geelen@gmail.com
Date2014-02-11 11:06 -0800
Message-ID<9881c34f-20f4-43ea-9f9c-e1bd7fec8400@googlegroups.com>
In reply to#65947
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.geelen@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 ?

[toc] | [prev] | [next] | [standalone]


#65954

FromGary Herron <gary.herron@islandtraining.com>
Date2014-02-11 11:26 -0800
Message-ID<mailman.6699.1392146784.18130.python-list@python.org>
In reply to#65949
On 02/11/2014 11:06 AM, luke.geelen@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

[toc] | [prev] | [next] | [standalone]


#65955

FromTim Chase <python.list@tim.thechases.com>
Date2014-02-11 13:28 -0600
Message-ID<mailman.6700.1392146887.18130.python-list@python.org>
In reply to#65949
On 2014-02-11 11:06, luke.geelen@gmail.com wrote:
> > > >   command1 = "sudo mpg321   
> >   
> > >   
> >   
> > > >   'http://translate.google.com/translate_tts?tl=en&q=%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



[toc] | [prev] | [next] | [standalone]


#65957

Fromluke.geelen@gmail.com
Date2014-02-11 11:54 -0800
Message-ID<30df1873-45fd-4960-9407-86b0da73f41d@googlegroups.com>
In reply to#65955
Op dinsdag 11 februari 2014 20:28:44 UTC+1 schreef Tim Chase:
> On 2014-02-11 11:06, luke.geelen@gmail.com wrote:
> 
> > > > >   command1 = "sudo mpg321   
> 
> > >   
> 
> > > >   
> 
> > >   
> 
> > > > >   'http://translate.google.com/translate_tts?tl=en&q=%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)

[toc] | [prev] | [next] | [standalone]


#65959

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2014-02-11 20:02 +0000
Message-ID<mailman.6702.1392148960.18130.python-list@python.org>
In reply to#65957
On 11/02/2014 19:54, luke.geelen@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

[toc] | [prev] | [next] | [standalone]


#65952

FromJussi Piitulainen <jpiitula@ling.helsinki.fi>
Date2014-02-11 21:14 +0200
Message-ID<qot61oltqbm.fsf@ruuvi.it.helsinki.fi>
In reply to#65947
luke.geelen@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.

[toc] | [prev] | [next] | [standalone]


#65953

FromGary Herron <gary.herron@islandtraining.com>
Date2014-02-11 11:20 -0800
Message-ID<mailman.6698.1392146445.18130.python-list@python.org>
In reply to#65947
On 02/11/2014 11:01 AM, luke.geelen@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

[toc] | [prev] | [next] | [standalone]


#65945

FromGary Herron <gary.herron@islandtraining.com>
Date2014-02-11 10:55 -0800
Message-ID<mailman.6694.1392144984.18130.python-list@python.org>
In reply to#65940
On 02/11/2014 10:37 AM, luke.geelen@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

[toc] | [prev] | [next] | [standalone]


#65946

Fromluke.geelen@gmail.com
Date2014-02-11 10:59 -0800
Message-ID<b31d9efd-11c0-410a-88b9-7774c0318c6a@googlegroups.com>
In reply to#65945
Op dinsdag 11 februari 2014 19:55:59 UTC+1 schreef Gary Herron:
> On 02/11/2014 10:37 AM, luke.geelen@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

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

[toc] | [prev] | [next] | [standalone]


#65950

FromGary Herron <gary.herron@islandtraining.com>
Date2014-02-11 11:09 -0800
Message-ID<mailman.6696.1392145780.18130.python-list@python.org>
In reply to#65946
On 02/11/2014 10:59 AM, luke.geelen@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

[toc] | [prev] | [next] | [standalone]


Page 1 of 2  [1] 2  Next page →

Back to top | Article view | comp.lang.python


csiph-web