[Tutor] calculator will not multiply

jadrifter jadrifter at gmail.com
Sun Jan 18 16:55:30 CET 2009


On Sun, 2009-01-18 at 10:37 -0500, David wrote:
> Everything else works + - / but not *
> why?
> thanks
> -david
> 
> 
> #!/usr/bin/python
> from __future__ import division
> import sys
> 
> 
> def add(x, y):
>      return x + y
> def sub(x, y):
>      return x - y
> def dev(x, y):
>      return x / y
> def mul(x, y):
>      return x * y
> def compute(arg1, arg2, arg3):
>      if sys.argv[2] == "+":
>          total = add(int(sys.argv[1]), int(sys.argv[3]))
>          print total
>      elif sys.argv[2] == "-":
>          total = sub(int(sys.argv[1]), int(sys.argv[3]))
>          print total
>      elif sys.argv[2] == "/":
>          total = dev(int(sys.argv[1]), int(sys.argv[3]))
>          print total
>      elif sys.argv[2] == "*":
>          total = mul(int(sys.argv[1]), int(sys.argv[3]))
>          print total
>      else:
>          print "oops"
> 
> compute(sys.argv[1], sys.argv[2], sys.argv[3])
> 
> 

It worked fine for me.  Don't forget to put quotes around the * sign or
the shell will substitute.

so:
./calc.py 2 '*' 9
or:
./calc.py 2 "*" 9
or:
./calc.py 2 \* 9
but not:
./calc.py 2 * 9

John Purser




More information about the Tutor mailing list