Help with my 8-year old son's first program. I'm stuck!

J

justinpmullins

My son is learning Python and I know nothing about computers.
He's written a simple calculator program that doesn't work. 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):
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")

Where is he going wrong?
Many thanks in advance
 
E

Ervin Hegedüs

Hello,

My son is learning Python and I know nothing about computers.
:)

He's written a simple calculator program that doesn't work. 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):
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")

Where is he going wrong?

what's your error message?

First, you have to put an interpreter in first line - if you're
on Linux/Unix:

#!/usr/bin/python

or some kind of that. I don't know what's the expected on Windows
:(.

Second, you defined a funcfion, called "a", and if you want to
see how does it work, you must call that (after you define it):

def a():
...
...

a()


Third, at the first statement (if op == str(d)) you're
referencing for an undefined variable (d), I think you would
put `if op == "d":', instad of that.


Good luck,

Ervin
 
J

justinpmullins

PS: At the first statement, we've also tried

op == "d":

But that doesn't work either.
 
P

Peter Otten

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.
 
D

Denis McMahon

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):
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")

a() is a function, but I can see nothing in the code that invokes the
function a()

Ignore the comment about needing to define the type of file on the first
line on linux, it's a red herring. You only need to do that (and possibly
chmod the file) on a *nix system if you want to execute it directly as a
command, rather than by calling the interpreter on it.

I suspect that if you remove the line:

"def a():"

and un-indent the rest of the text, the program will run just fine.
 
J

justinpmullins

Thanks Peter, that did the trick.
You've got here a very happy 8-year old and a mighty relieved 46-year old!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top