[Tutor] calculator will not multiply

Marc Tompkins marc.tompkins at gmail.com
Sun Jan 18 19:45:13 CET 2009


On Sun, Jan 18, 2009 at 7:37 AM, David <david at abbottdavid.com> 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 works for me under Windows XP, so I suspect that the hints you've
received about * being expanded by your shell are correct.  However, I had
an idea - rather than requiring your user to put quotes around the "*", why
not do this:

   elif sys.argv[2] in ["*","x","X"]:
       total = mul(int(sys.argv[1]), int(sys.argv[3]))
       print total

This could be a problem if you wanted to develop your calculator into a
full-fledged scientific calculator, but otherwise...


-- 
www.fsrtechnologies.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090118/2e57f5d0/attachment.htm>


More information about the Tutor mailing list