[Tutor] python problem
Andrei
project5 at redrival.net
Thu May 26 20:40:40 CEST 2005
Feziwe Mpondo <feziwe <at> sanbi.ac.za> writes:
> i'm trying to write a code that handle errors help
I think the indentation was screwed up.
> while menu_choice != 5:
> menu_choice = input("Type in a number (1-5):")
I'd avoid using input. Use raw_input instead.
> break
break stops a loop. So you display the menu once and stop immediately after the
user has selected an action. That can't be right.
> except(TypeError,NameError):
Exceptions are caught in a try except block, where the code that might
malfunction comes after try, and the code to handle the malfunctioning comes
after except. It therefore looks something like this:
userinput = raw_input('Choice: ')
try: # userinput may or may not be valid
choice = int(userinput) # might fail if userinput is random text
except (ValueError): # what to do if couldn't convert to integer
print 'Not a number'
choice = 5
Yours,
Andrei
More information about the Tutor
mailing list