### Thread: Need a second look

1. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2003
Location
Canada
Posts
185
Rep Power
0

#### Need a second look

I had this code working before but then i did something and it wiill not accept the correct responses. it says it is incorrect. I'm afraid i 've been looking at it too long, because i can't find the bug
Code:
```import random

def menu():
print "Main Menu"
print "========="

print "1) Addition"
print "2) Subtraction"
print "3) Multiplication"
print "4) Division"
choice =  raw_input(">> ")
if choice =="1":
Add()
elif choice == "2":
Sub()
elif choice == "3":
Mul()
elif choice == "4":
Div()
else:
print "Invalid choice"
menu()

def Add():

adden = random.randrange(0,100)
adden2 = random.randrange(0,100)
sum = adden + adden2
print "What is the sum of these integers"
print "%5d" %adden
print "%-1s" %("+")
print "%5d" %adden2
print "-----------"
ans = raw_input(">>")
if ans == sum:
#correct +=1
print "Correct"
Add()
else:
#wrong +=1
print "Incorrect, the answer is:  %d" % sum
Add()

def Sub( ):
minuend = random.randrange(0,100)
subtruend =random.randrange(0,minuend)
diff = (minuend - subtruend)
print "What is the difference of these integers:"
print "%5d" %minuend
print "-"
print "%5d" %subtruend
print "-----------"
ans = raw_input(">>")
if ans == diff:
correct +=1
print "Correct"
Sub()
else:
#wrong+=1
print "Incorrect the answer is %d" %diff
Sub()

def Mul():
"""The multiplication suff"""

menu()```
Last edited by caroundw5h; March 8th, 2004 at 02:47 PM.
2. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Mar 2002
Posts
89
Rep Power
16
Originally Posted by caroundw5h
I had this code working before but then i did something and it wiill not accept the correct responses. it says it is incorrect. I'm afraid i 've been looking at it too long, because i can't find the bug
Code:
```import random

def menu():
print "Main Menu"
print "========="

print "1) Addition"
print "2) Subtraction"
print "3) Multiplication"
print "4) Division"
choice =  raw_input(">> ")
if choice =="1":
Add()
elif choice == "2":
Sub()
elif choice == "3":
Mul()
elif choice == "4":
Div()
else:
print "Invalid choice"
menu()

def Add():

adden = random.randrange(0,100)
adden2 = random.randrange(0,100)
sum = adden + adden2
print "What is the sum of these integers"
print "%5d" %adden
print "%-1s" %("+")
print "%5d" %adden2
print "-----------"
ans = raw_input(">>")
if ans == sum:
#correct +=1
print "Correct"
Add()
else:
#wrong +=1
print "Incorrect, the answer is:  %d" % sum
Add()

def Sub( ):
minuend = random.randrange(0,100)
subtruend =random.randrange(0,minuend)
diff = (minuend - subtruend)
print "What is the difference of these integers:"
print "%5d" %minuend
print "-"
print "%5d" %subtruend
print "-----------"
ans = raw_input(">>")
if ans == diff:
correct +=1
print "Correct"
Sub()
else:
#wrong+=1
print "Incorrect the answer is %d" %diff
Sub()

def Mul():
"""The multiplication suff"""

menu()```
try changing your

ans = raw_input(">> ")

to

int(ans = raw_input(">> "))

in the addition and substration function
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2003
Posts
217
Rep Power
0
Originally Posted by caroundw5h
Code:
```def Sub( ):
minuend = random.randrange(0,100)
subtruend =random.randrange(0,minuend)
diff = (minuend - subtruend)
print "What is the difference of these integers:"
print "%5d" %minuend
print "-"
print "%5d" %subtruend
print "-----------"
ans = raw_input(">>")
if ans == diff:
correct +=1
print "Correct"
Sub()
else:
#wrong+=1
print "Incorrect the answer is %d" %diff
Sub()```
I don't think that that uncommented correct += 1 is helping much since you havn't defined it anywhere
4. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2003
Posts
7
Rep Power
0
Code:
```import sys
import random
(...)
def Add():

adden = random.randrange(0,100)
adden2 = random.randrange(0,100)
sum = adden + adden2
print "What is the sum of these integers"
print "%5d" %adden
print "%-1s" %("+")
print "%5d" %adden2
print "-----------"
try:
# input() for integer only
ans = input(">>")
except: #if not an integer
# quit the program
sys.exit( 1 )
if ans == sum:
#correct +=1
print "Correct"
Add()
else:
#wrong +=1
print "Incorrect, the answer is:  %d" % sum
Add()```
Here the program will stop if the input for the "ans" variable is not an integer,
perhaps the problem is that you call the Add() function recursively without
a test to stop the function.
if it works make similar modification to sub() function.

regards,
Alexandre
5. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2003
Location
Canada
Posts
185
Rep Power
0
try changing your

ans = raw_input(">> ")

to

int(ans = raw_input(">> "))

in the addition and substration function
I tried typecasting even before i posted the code
Code:
`ans = int(raw_input(">>))`
No effect.

perhaps the problem is that you call the Add() function recursively without
a test to stop the function.
Thanks but i doubt the lack of exception handling has anyting to do with it. Also , if it was the recursive function call, that is called only after it has tested if ans==sum and as such should not interfere with the correct answer. that would suggest that python does preprocessing to the sourcecode, which it doesn't. Python is interpreted and as such interprets as it goes along. I think Java does differently, but that is outside the scope of this thread.

funny thing is i didn't prototype this in python, i actully did if first in C and i have no problems with it. Its really starting to irritate me though. Thank you all for your suggestions. \$20 bucks to whoever can solve it.

6. A small rewrite of your program just for fun, but it does seem to work fine.

Code:
```#!/usr/bin/env python

from random import randint

def add():
number1 = randint(0, 100)
number2 = randint(0, 100)

stored = number1 + number2
answer = int(raw_input('%d + %d >>> ' % (number1, number2)))

if answer == stored:
print 'Great!!! (%d + %d = %d)' % (number1, number2, stored)
else:
print 'Sorry!!! (%d + %d = %d)' % (number1, number2, stored)

def sub():
number1 = randint(0, 100)
number2 = randint(0, 100)

stored = number1 - number2
answer = int(raw_input('%d - %d >>> ' % (number1, number2)))

if answer == stored:
print 'Great!!! (%d - %d = %d)' % (number1, number2, stored)
else:
print 'Sorry!!! (%d - %d = %d)' % (number1, number2, stored)

if __name__ == '__main__':

print 'Menu...'
print '1. addition'
print '2. subtraction'
print '3. quit'

while True:

option = raw_input('Enter a number>>> ')

if option == '1':
add()
elif option == '2':
sub()
elif option == '3':
break```
Have fun with it,

Mark.

(Code highlighting by Grims py2html)
Last edited by netytan; March 8th, 2004 at 06:52 PM.
7. Wells its probably something very simple , its always the way... sure you'll get it though. Anyway Xx was right though, the whole += thing will cause an error if it gets that far, simply because the variable isnt iniciated in your code.

that would suggest that python does preprocessing to the sourcecode, which it doesn't. Python is interpreted and as such interprets as it goes along. I think Java does differently, but that is outside the scope of this thread.
Actually, as i understand it Python compiled the program to an intermediate language called byte-code (on the fly) before executing it. So if you count that as preprocessing then it does .

Mark.
Last edited by netytan; March 8th, 2004 at 07:02 PM.
8. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Mar 2002
Posts
89
Rep Power
16
well I don't know what's going on in your script but wrapping the

ans = raw_input(">> ")

with an int() makes the script run perfectly for me

Code:
```import random

def menu():
print "Main Menu"
print "========="

print "1) Addition"
print "2) Subtraction"
print "3) Multiplication"
print "4) Division"
choice =  raw_input(">> ")
if choice =="1":
Add()
elif choice == "2":
Sub()
elif choice == "3":
Mul()
elif choice == "4":
Div()
else:
print "Invalid choice"
menu()

def Add():

adden = random.randrange(0,100)
adden2 = random.randrange(0,100)
sum = adden + adden2
print "What is the sum of these integers"
print "%5d" %adden
print "%-1s" %("+")
print "%5d" %adden2
print "-----------"
ans = int(raw_input(">>"))
if ans == sum:
#correct +=1
print "Correct"
Add()
else:
#wrong +=1
print "Incorrect, the answer is:  %d" % sum
Add()

def Sub( ):
minuend = random.randrange(0,100)
subtruend =random.randrange(0,minuend)
diff = (minuend - subtruend)
print "What is the difference of these integers:"
print "%5d" %minuend
print "-"
print "%5d" %subtruend
print "-----------"
ans = int(raw_input(">>"))
if ans == diff:
#correct +=1
print "Correct"
Sub()
else:
#wrong+=1
print "Incorrect the answer is %d" %diff
Sub()

def Mul():
"""The multiplication suff"""

menu()```
9. Originally Posted by jimmy2k1
well I don't know what's going on in your script but wrapping the

ans = raw_input(">> ")

with an int() makes the script run perfectly for me
hehe,it sounds like you know waaay more than me,but i recently learned this:

the reason it worked when you wrapped it with int() is because you were,as you said earlier in the thread, typecasting the input as an integer.even if the input is a number,w/o typecasting the input as a integer,Python treats the input as a string.and because it was being treated like a string,you prob got a ValueError and it didn't work until you typecasted the input as a integer.