Hi Guys,

I'm a beginner programming with Python. This is my first program. The purpose of this program is to add two numbers and then give me the result. However, after that I want for it to ask me if I want to continue (y) or (n). I came with some difficulties and every change I make it gives me an error message. This is what I have so far.

print "Let's learn how to add"
x = int(input ("Give me the first number: "))
y = int(input ("Give me the second number: "))
print "The result is", (x+y)
userinput = input('Want to continue adding?\nA) Y\nB) N\n').lower()
if userinput == 'a':
print " Let's continue learning how to add"
x = int(input ("Give me the first number: "))
y = int(input ("Give me the second number: "))
print "The result is", (x+y)
if userinput == 'b':
print ('\nNow quitting program..')
time.sleep(3)
else:

I really appreciate your feedback. Will comment on threads once I understand what I'm doing. :) Thanks.

4
Contributors
7
Replies
10
Views
7 Years
Discussion Span
Last Post by eric_arambula

When you are asking a string to the user, use raw_input instead of input (with python 2). Here is a working version where I added a few while statements

import time
print "Let's learn how to add"
quit = False
while not quit:
x = int(input ("Give me the first number: "))
y = int(input ("Give me the second number: "))
print "The result is", (x+y)
while True:
userinput = raw_input('Want to continue adding?\nA) Y\nB) N\n').lower()
if userinput == 'a':
print " Let's continue learning how to add"
break
elif userinput == 'b':
print ('\nNow quitting program..')
time.sleep(3)
quit = True
break
else:

When you are asking a string to the user, use raw_input instead of input (with python 2). Here is a working version where I added a few while statements

import time
print "Let's learn how to add"
quit = False
while not quit:
x = int(input ("Give me the first number: "))
y = int(input ("Give me the second number: "))
print "The result is", (x+y)
while True:
userinput = raw_input('Want to continue adding?\nA) Y\nB) N\n').lower()
if userinput == 'a':
print " Let's continue learning how to add"
break
elif userinput == 'b':
print ('\nNow quitting program..')
time.sleep(3)
quit = True
break
else:

Thanks. I see what I was missing. When can I use Raw input ?

Thanks. I see what I was missing. When can I use Raw input ?

In python 2.x is best to always use raw_input.
input() is i mix off eval() and raw_input() in python 2.x that return an integer.

Python 3.x has only input() work the same way as raw_input in python 2.x(return a string)
So you may ask,what if a need a integer from user input?

IDLE 2.6.5
>>> my_input = raw_input('Type something: ')
Type something: test
>>> type(my_input)
<type 'str'>
>>> my_input = raw_input('Type something: ')
Type something: 5
>>> my_input = int(my_input)
>>> my_input
5
>>> type(my_input)
<type 'int'>
>>> #you can make raw_input return an integer
>>> my_input = int(raw_input('Type something: '))
Type something: 8
>>> my_input
8
>>> type(my_input)
<type 'int'>
>>>

Edited by snippsat: n/a

Ok. I added a menu now. Everything working however, I want for it to ask me Want to continue adding (y) or (n). If no go to main menu. What do you think ?

import time
Choose the operation to do (1-2):
2) Substract

3) Quit
"""
while operation !=3:
if operation ==1:
x = int(raw_input ("Give me the first number: "))
y = int(raw_input("Give me the second number: "))
print "The result is", (x+y)
while True:
userinput = raw_input ("Want to continue adding?\nA) y\nB) n\n")
if userinput == 'y':
x = int(raw_input ("Give me the first number: "))
y = int(raw_input("Give me the second number: "))
print "The result is", (x+y)
if operation ==2:
x = int(raw_input ("Give me the first number: "))
y = int(raw_input ("Give me the second number: "))
print "The result is", (x-y)
else:
print "Not a valid operation, try again"

What am I missing ? :S

Ok. I added a menu now. Everything working however, I want for it to ask me Want to continue adding (y) or (n). If no go to main menu. What do you think ?

import time ## why this?
Choose the operation to do (1-2):
2) Substract

3) Quit
"""
while operation !=3:
if operation ==1:
x = int(raw_input ("Give me the first number: "))
y = int(raw_input("Give me the second number: "))
print "The result is", (x+y)
while True:
userinput = raw_input ("Want to continue adding?\nA) y\nB) n\n")
if userinput == 'y':
x = int(raw_input ("Give me the first number: "))
y = int(raw_input("Give me the second number: "))
print "The result is", (x+y)
if operation ==2:
x = int(raw_input ("Give me the first number: "))
y = int(raw_input ("Give me the second number: "))
print "The result is", (x-y)
else:
print "Not a valid operation, try again"

What am I missing ? :S

At least you were missing code tags ((code) button before paste you know) And after I put them to quote, I see that the codes indenting is messed up now. Could you redo, pasting the code properly?

Edited by mike_2000_17: Fixed formatting

Dont mix indentations use always 4 space.
Look into function,when you write all in global space it get soon difficult if you want to jump back and fourth between part of the code.

Choose the operation to do (1-2):
2) Substract
3) Quit
"""

x = int(raw_input("Give me the first number: "))
y = int(raw_input("Give me the second number: "))
print "The result is", (x+y)

def Substract():
x = int(raw_input ("Give me the first number: "))
y = int(raw_input("Give me the second number: "))
print "The result is", (x-y)

def main():
'''this is called dok string info about function'''
while True:
if operation == '1':
elif operation == '2':
Substract()
elif operation == '3':
return
else:
print 'Not correct input,try again'

if __name__ == '__main__':
main()

Here you fall back into main when a function is finish.
Then you dont neeed ask this userinput = raw_input ("Want to continue adding?\nA) y\nB) n\n") .
Because user can just type 1 again.

Edited by snippsat: n/a

Thanks to everybody for all your help. I think I have it now. :D