Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #64735 > unrolled thread

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

Started byjustinpmullins@gmail.com
First post2014-01-25 02:02 -0800
Last post2014-01-25 11:35 +0000
Articles 6 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  Help with my 8-year old son's first program. I'm stuck! justinpmullins@gmail.com - 2014-01-25 02:02 -0800
    Re: Help with my 8-year old son's first program. I'm stuck! Ervin Hegedüs <airween@gmail.com> - 2014-01-25 11:24 +0100
    Re: Help with my 8-year old son's first program. I'm stuck! justinpmullins@gmail.com - 2014-01-25 02:32 -0800
    Re: Help with my 8-year old son's first program. I'm stuck! Peter Otten <__peter__@web.de> - 2014-01-25 11:41 +0100
      Re: Help with my 8-year old son's first program. I'm stuck! justinpmullins@gmail.com - 2014-01-25 07:58 -0800
    Re: Help with my 8-year old son's first program. I'm stuck! Denis McMahon <denismfmcmahon@gmail.com> - 2014-01-25 11:35 +0000

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

Fromjustinpmullins@gmail.com
Date2014-01-25 02:02 -0800
SubjectHelp with my 8-year old son's first program. I'm stuck!
Message-ID<453f851d-815d-4bdd-b591-71cafc95084f@googlegroups.com>
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

[toc] | [next] | [standalone]


#64736

FromErvin Hegedüs <airween@gmail.com>
Date2014-01-25 11:24 +0100
Message-ID<mailman.5971.1390645712.18130.python-list@python.org>
In reply to#64735
Hello,

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

[toc] | [prev] | [next] | [standalone]


#64737

Fromjustinpmullins@gmail.com
Date2014-01-25 02:32 -0800
Message-ID<5dce8685-9d73-4519-99e4-d79d62fd0bf4@googlegroups.com>
In reply to#64735
PS: At the first statement, we've also tried 
 
op == "d":

But that doesn't work either.



On Saturday, January 25, 2014 10:02:15 AM UTC, justinp...@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?
> 
> Many thanks in advance

[toc] | [prev] | [next] | [standalone]


#64738

FromPeter Otten <__peter__@web.de>
Date2014-01-25 11:41 +0100
Message-ID<mailman.5972.1390646490.18130.python-list@python.org>
In reply to#64735
justinpmullins@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.

[toc] | [prev] | [next] | [standalone]


#64746

Fromjustinpmullins@gmail.com
Date2014-01-25 07:58 -0800
Message-ID<fd98af71-75b5-495c-afe0-b8dd23ca07c2@googlegroups.com>
In reply to#64738
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@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.

[toc] | [prev] | [next] | [standalone]


#64741

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2014-01-25 11:35 +0000
Message-ID<lc07h4$5fh$2@dont-email.me>
In reply to#64735
On Sat, 25 Jan 2014 02:02:15 -0800, justinpmullins wrote:

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

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web