|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here! |
|
#1
|
|||
|
|||
|
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()
__________________
"In theory, there is no difference between theory and practice. But, in practice, there is."
Last edited by caroundw5h : March 8th, 2004 at 01:47 PM. |
|
#2
|
|||
|
|||
|
Quote:
try changing your ans = raw_input(">> ") to int(ans = raw_input(">> ")) in the addition and substration function |
|
#3
|
|||
|
|||
|
Quote:
I don't think that that uncommented correct += 1 is helping much since you havn't defined it anywhere |
|
#4
|
|||
|
|||
|
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
|
||||
|
||||
|
Quote:
I tried typecasting even before i posted the code Code:
ans = int(raw_input(">>))
Quote:
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 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.Quote:
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
|
|||
|
|||
|
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
|
||||
|
||||
|
Quote:
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.
__________________
It is not important if the glass is half full or half empty.What is important,is who has been drinking from MY glass?!?!? |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Need a second look |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|