Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 22nd, 2006, 8:26 PM   #1
somehollis
Programmer
 
somehollis's Avatar
 
Join Date: May 2006
Location: Memphis, TN
Posts: 31
Rep Power: 0 somehollis is on a distinguished road
Send a message via AIM to somehollis
Question 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)
somehollis is offline   Reply With Quote
Old Jun 22nd, 2006, 8:35 PM   #2
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,869
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
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?
Sane is offline   Reply With Quote
Old Jun 22nd, 2006, 8:46 PM   #3
somehollis
Programmer
 
somehollis's Avatar
 
Join Date: May 2006
Location: Memphis, TN
Posts: 31
Rep Power: 0 somehollis is on a distinguished road
Send a message via AIM to somehollis
!!! 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!
somehollis is offline   Reply With Quote
Old Jun 22nd, 2006, 8:49 PM   #4
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,869
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
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.
Sane is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:12 AM.

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