Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Python (http://www.programmingforums.org/forum43.html)
-   -   Why did %S break my if statement? (http://www.programmingforums.org/showthread.php?t=10493)

somehollis Jun 22nd, 2006 9:26 PM

Why did %S break my if statement?
 
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)


Sane Jun 22nd, 2006 9:35 PM

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?

somehollis Jun 22nd, 2006 9:46 PM

!!! 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 ""

Thanks!

Sane Jun 22nd, 2006 9:49 PM

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. :P


All times are GMT -5. The time now is 8:03 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC