![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
|
I was working on an if statement inside a function (see below for code) and made the small typo of %S instead of %s inside of one of my print statements. For some reason this caused the program to exit the function immediately. Can someone explain exactly what that is doing?
I don't know that it matters, but here is the function with the location of the (now former) typo in red. The gratuitous print statments that I used to help me debug are still in there, so you can ignore them. def remove_person():
name = input_list()
if dbase.has_key(name):
print 'dbase has %s' % (name)
print len(dbase[name])
if not len(dbase[name]) == 0:
print "%s still has items on loan." % (name)
certain = raw_input("Delete anyway? (y/N):")
if re.match('^[yY][eE]?[sS]?$', certain):
del dbase[name]
print 'deleted'
else:
print "not deleted"
print 'delete path NOT taken'
else:
print 'delete path taken'
del dbase[name]
else:
print "%s was not found." % (name) |
|
|
|
|
|
#2 |
|
Programming Guru
![]() |
What do you mean it "exited the function"? It should be raising a ValueError, since %S is an unsupported format character. Is this Python 2.4?
|
|
|
|
|
|
#3 |
|
Programmer
|
!!! It makes so much sense now. It was rasing a ValueError, but that function was run from inside a try block. just before the function is called, I'd had the program try int(some.user.input) and had a ValueError exception ready in case of bad input.
That function was called at option[choice]() below. while choice != 9: # main menu loop
menu()
try:
choice = int(raw_input("Selection:"))
if choice != 9:
try:
option[choice]()
except KeyError:
print "Invalid choice: please enter a number from the menu"
except ValueError:
print "Invalid choice: please enter a number"
print "" |
|
|
|
|
|
#4 |
|
Programming Guru
![]() |
Ahh. Excellent. What a sucker those things can be. My first thought was you had it in a try, since you didn't mention raising any errors. But I couldn't see that anywhere in your code. Very well.
![]() |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|