The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Need a second look
Discuss Need a second look in the Python Programming forum on Dev Shed. Need a second look Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

March 8th, 2004, 01:44 PM
|
|
Contributing User
|
|
Join Date: Oct 2003
Location: Canada
Posts: 185
Time spent in forums: 20 h 44 m 50 sec
Reputation 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()
__________________
"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.
|

March 8th, 2004, 02:28 PM
|
|
Contributing User
|
|
Join Date: Mar 2002
Posts: 89
Time spent in forums: 8 h 48 m 50 sec
Reputation Power: 12
|
|
Quote: | 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
|

March 8th, 2004, 02:37 PM
|
|
Registered User
|
|
Join Date: Aug 2003
Posts: 217
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
Quote: | 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
|

March 8th, 2004, 03:22 PM
|
|
Registered User
|
|
Join Date: Dec 2003
Posts: 7
Time spent in forums: < 1 sec
Reputation 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
|

March 8th, 2004, 05:12 PM
|
|
Contributing User
|
|
Join Date: Oct 2003
Location: Canada
Posts: 185
Time spent in forums: 20 h 44 m 50 sec
Reputation Power: 0
|
|
Quote:
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.
Quote: 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.

|

March 8th, 2004, 05:36 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
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)
__________________
programming language development: www.netytan.com – Hula
Last edited by netytan : March 8th, 2004 at 05:52 PM.
|

March 8th, 2004, 06:00 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
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: | 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.
|

March 8th, 2004, 09:00 PM
|
|
Contributing User
|
|
Join Date: Mar 2002
Posts: 89
Time spent in forums: 8 h 48 m 50 sec
Reputation Power: 12
|
|
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()
|

March 9th, 2004, 05:41 AM
|
 |
Contributing User
|
|
Join Date: Jan 2004
Posts: 103
Time spent in forums: 3 Days 1 h 17 m 20 sec
Reputation Power: 10
|
|
Quote: | 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.
__________________
It is not important if the glass is half full or half empty.What is important,is who has been drinking from MY glass?!?!?
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|