Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Try It Free
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!
  #1  
Old March 8th, 2004, 01:44 PM
caroundw5h caroundw5h is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Canada
Posts: 181 caroundw5h User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 55 m 34 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.

Reply With Quote
  #2  
Old March 8th, 2004, 02:28 PM
jimmy2k1 jimmy2k1 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2002
Posts: 89 jimmy2k1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 48 m 50 sec
Reputation Power: 7
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

Reply With Quote
  #3  
Old March 8th, 2004, 02:37 PM
XxChris XxChris is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 217 XxChris User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #4  
Old March 8th, 2004, 03:22 PM
hidux hidux is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 7 hidux User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to hidux Send a message via AIM to hidux
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

Reply With Quote
  #5  
Old March 8th, 2004, 05:12 PM
caroundw5h caroundw5h is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Canada
Posts: 181 caroundw5h User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 55 m 34 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.



Reply With Quote
  #6  
Old March 8th, 2004, 05:36 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
  #7  
Old March 8th, 2004, 06:00 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
  #8  
Old March 8th, 2004, 09:00 PM
jimmy2k1 jimmy2k1 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2002
Posts: 89 jimmy2k1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 48 m 50 sec
Reputation Power: 7
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()

Reply With Quote
  #9  
Old March 9th, 2004, 05:41 AM
Boceifus's Avatar
Boceifus Boceifus is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 93 Boceifus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 2 h 5 m 27 sec
Reputation Power: 5
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?!?!?

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Need a second look


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |