×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Error Handling (Newbie)

Error Handling (Newbie)

Error Handling (Newbie)

(OP)
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` + "%"

RE: Error Handling (Newbie)

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.


RE: Error Handling (Newbie)

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 smile

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close