Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error Handling (Newbie)

Status
Not open for further replies.

Mobidoy

Technical User
Oct 12, 2006
3
0
0
CA
Hello all,

i am a beginner at programming. I am trying to code a little program for my daughter t help her learn her multiplication. Everything works fine but if instead of putting a number for input, you just press enter, i get an error and it crash. I tried to find a solution to put a string to ask a good answer but just cant get it. Can someone help me please

Here is the code:

Code:
import random

test = 0
note = 0
while test < 20:
    multipliant = random.randrange(1,13)
    multiplieur = random.randrange(1,13)
    quotient = multipliant*multiplieur
    print `multipliant` + " X " + `multiplieur` + " = "
    try:
        reponse = input('Votre réponse ?:')
    except:
        print "Entrez une réponse :"
    else:
        test = test + 1
    if reponse == quotient:
        note = note + 1
        print "Bravo"
    else:
        print "Desoler la bonne réponse est: " + `quotient`
        reponse = input("Votre réponse ?:")            
print "Vous avez réussi " + `note` + " questions sur 20"
print `note*5` + "%"
 
Mobidoy,

The "input()" command requires that something other than an empty line be entered. So, when you enter an empty line, the "try" fails and execution continues in the "except" part, so the "reponse" variable is not set or defined, so later when the "if reponse == quotient" is executed, you get the error:

Traceback (most recent call last):
File "somename.py", line 16, in ?
if reponse == quotient:
NameError: name 'reponse' is not defined


You need to set 'reponse' to an initial value or to do better error handling with "try/except" pairs.


 
Instead of input() I would better use raw_input(), This works
Code:
import random

test = 0
note = 0
while test < 20:
    multipliant = random.randrange(1,13)
    multiplieur = random.randrange(1,13)
    quotient = multipliant*multiplieur
    print `multipliant` + " X " + `multiplieur` + " = "
    try:
        reponse = raw_input('Votre réponse ?:')
    except:
        print "Entrez une réponse :"
    else:
        test = test + 1
    if reponse == quotient:
        note = note + 1
        print "Bravo"
    else:
        print "Desoler la bonne réponse est: " + `quotient`
        reponse = raw_input("Votre réponse ?:")            
print "Vous avez réussi " + `note` + " questions sur 20"
print `note*5` + "%"
but I'dont know if this is what you want because I'dont understand french (or is it italian?). This is probably not good because the program comes then never to your question in the try-block. But maybe this is what you want
Code:
import random

num_of_tests = 5
note = 0
test = 1
while test <= num_of_tests:
    print "Test Question Number %2d" % test
    multipliant = random.randrange(1,13)
    multiplieur = random.randrange(1,13)
    quotient = multipliant*multiplieur
    print "%s X %s =" %(multipliant, multiplieur)
    reponse = raw_input('Votre réponse ?:')
    if not reponse.isdigit():
       # when reponse is not a number
       print "Entrez une réponse :"
    else:
       test = test + 1
       # convert reponse from string to integer and compare with quotient
       if int(reponse) == quotient:
          note = note + 1
          print "Bravo"
       else:
          print "Desoler la bonne réponse est: " + `quotient`
       
print "Vous avez réussi %d questions sur %d" % (note, num_of_tests)
print "This is %d %%" % (note*100/num_of_tests)
I used some 'features' like string-integer conversion and formatted output to give you a sample :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top