Help with my 8-year old son's first program. I'm stuck!
justinpmullins at gmail.com
justinpmullins at gmail.com
Sat Jan 25 10:58:40 EST 2014
Thanks Peter, that did the trick.
You've got here a very happy 8-year old and a mighty relieved 46-year old!!
On Saturday, January 25, 2014 10:41:20 AM UTC, Peter Otten wrote:
> justinpmullins at gmail.com wrote:
>
>
>
> > My son is learning Python and I know nothing about computers.
>
> > He's written a simple calculator program that doesn't work.
>
>
>
> Normally you are supposed to explain what you or your son expect and what
>
> you get instead. If Python ends with an error you should paste that into
>
> your message, e. g.:
>
>
>
> Traceback (most recent call last):
>
> File "calculator.py", line 29, in <module>
>
> a()
>
> File "calculator.py", line 14, in a
>
> if op == str(d):
>
> NameError: global name 'd' is not defined
>
>
>
> Also, we need to know if you are using Python 2 or Python 3. Sometimes even
>
> the exact version is important. You can find it out with
>
>
>
> $ python3 -V
>
> Python 3.2.2
>
>
>
> > For the life
>
> > of me, I can't see why. Any help gratefully received. Here's his code:
>
>
>
> > def a():
>
> > import sys
>
> > print("welcome to the calculation")
>
> > print("please type a number")
>
> > one = int(sys.stdin.readline())
>
> > print("type d for division,")
>
> > print("type m for multiplication,")
>
> > print("type s for subtraction,")
>
> > print("and type p for plus")
>
> > op = (sys.stdin.readline())
>
> > print("%s selected" % op)
>
> > print("please enter another number")
>
> > two = int(sys.stdin.readline())
>
> > if op == str(d):
>
>
>
> The name d is defined nowhere in your script. That line should be
>
>
>
> if op == "d":
>
>
>
> similar to the `elif`s that follow.
>
>
>
> > out == one / two
>
>
>
> You want to assign to out but you are actually comparing out to one / two.
>
> Change the line (and similar lines below) to a single =, e. g.
>
> out = one / two
>
>
>
> > print("the answer is %s" % out)
>
> > elif op == "m":
>
> > out == one * two
>
> > print("the answer is %s" % out)
>
> > elif op == "s":
>
> > out == one - two
>
> > print("the answer is %s" % out)
>
> > elif op == "p":
>
> > out == one + two
>
> > print("the answer is %s" % out)
>
> > else:
>
> > print("huh")
>
>
>
> Change the above line to
>
>
>
> print("Unknown op=%r" % op)
>
>
>
> and add a function invocation
>
>
>
> a()
>
>
>
> > Where is he going wrong?
>
> > Many thanks in advance
>
>
>
> When you run the script with my modifications
>
>
>
> $ python3 calculator.py
>
> welcome to the calculation
>
> please type a number
>
> 10
>
> type d for division,
>
> type m for multiplication,
>
> type s for subtraction,
>
> and type p for plus
>
> m
>
> m
>
> selected
>
> please enter another number
>
> 20
>
> Unknown op='m\n'
>
>
>
> you see that what you supposed to be an "m" is actually an "m" followed by a
>
> newline. The readline() method reads a line including the final newline.
>
> You can remove that by changing the line
>
>
>
> op = (sys.stdin.readline())
>
>
>
> to
>
>
>
> op = sys.stdin.readline().strip()
>
>
>
> but a more straightforward approach would be to replace all occurences of
>
>
>
> sys.stdin.readline()
>
>
>
> with
>
>
>
> input() # if you are using Python 3
>
>
>
> or
>
>
>
> raw_input() # if yo are using Python 2.
More information about the Python-list
mailing list