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

Ervin Hegedüs airween at gmail.com
Sat Jan 25 05:24:33 EST 2014


Hello,

On Sat, Jan 25, 2014 at 02:02:15AM -0800, 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. 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


-- 
I � UTF-8



More information about the Python-list mailing list