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


Groups > comp.lang.python > #65942

Re: Flag control variable

From Peter Otten <__peter__@web.de>
Subject Re: Flag control variable
Date 2014-02-11 19:51 +0100
Organization None
References <baffb663-4f6d-4580-ac5d-d99b4aaf3c40@googlegroups.com> <5019eb5d-ebda-4417-ab2d-9a58afcdd186@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.6693.1392144693.18130.python-list@python.org> (permalink)

Show all headers | View raw


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 +

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web