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

Join Date
Oct 2003
Location
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

print "========="

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

print "What is the sum of these integers"
print "%-1s" %("+")
print "-----------"
ans = raw_input(">>")
if ans == sum:
#correct +=1
print "Correct"
else:
#wrong +=1
print "Incorrect, the answer is:  %d" % sum

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

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

Join Date
Mar 2002
Posts
89
Rep Power
17
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

print "========="

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

print "What is the sum of these integers"
print "%-1s" %("+")
print "-----------"
ans = raw_input(">>")
if ans == sum:
#correct +=1
print "Correct"
else:
#wrong +=1
print "Incorrect, the answer is:  %d" % sum

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

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

print "What is the sum of these integers"
print "%-1s" %("+")
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"
else:
#wrong +=1
print "Incorrect, the answer is:  %d" % sum
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
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

number1 = randint(0, 100)
number2 = randint(0, 100)

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

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

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

if __name__ == '__main__':

print '2. subtraction'
print '3. quit'

while True:

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

if option == '1':
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 05: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 06:02 PM.
8. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Mar 2002
Posts
89
Rep Power
17
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

print "========="

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

print "What is the sum of these integers"
print "%-1s" %("+")
print "-----------"
ans = int(raw_input(">>"))
if ans == sum:
#correct +=1
print "Correct"
else:
#wrong +=1
print "Incorrect, the answer is:  %d" % sum

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