User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 428,384 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,597 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser: Programming Forums
Views: 207 | Replies: 2
Reply
Join Date: Apr 2008
Posts: 26
Reputation: FreezeBlink is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
FreezeBlink FreezeBlink is offline Offline
Light Poster

Can someone help me improve this function?

  #1  
May 23rd, 2008
At various points in my text-based programs, I've had to write a function for use in multiple-choice menus. It needs to have the following qualities:
  • Ignores non-number input, repeating the request if it is given a string.
  • Can be given upper and lower limits on the acceptable numbers, repeating the request if the number the user enters is outside these limits.
  • Either or both limits must support being given the value False, in which case that limit will not be applied.

I already have a bit of code for this, but I hate its sheer ugliness and clumsiness. Here it is:
  1. def getChoice(bottom,cap):
  2. testOne = False
  3. testTwo = False
  4. while testOne == False or testTwo == False:
  5.  
  6. choice = raw_input()
  7.  
  8. try:
  9. choice = int(choice)
  10. testOne = True
  11. except:
  12. print "Please give a valid answer."
  13. testOne = False
  14.  
  15. if (testOne == True):
  16. if ((choice < bottom) and (bottom != False)) or ((choice > cap) and (cap != False)) or (choice != round(choice)):
  17. print "Please give a valid answer."
  18. testTwo = False
  19. else:
  20. testTwo = True
  21.  
  22. return choice

I'm not even sure if it's bug-free. Anyone able to suggest improvements or better implementations?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2008
Posts: 3
Reputation: tudor.petru is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
tudor.petru tudor.petru is offline Offline
Newbie Poster

Re: Can someone help me improve this function?

  #2  
May 23rd, 2008
Hi,

I'm just starting with Python, so I took your question like an exercise...here is what I did:

def getChoice(bottom = False,cap = False):

    if (bottom and cap) and (bottom > cap): #invalid args
        raise 1 
    
    msg = 'Enter a number'
    
    if bottom:
        msg += ' greater than ' + str(bottom)
    if cap:
        msg += ' smaller than ' + str(cap)


    choice = False
    
    while choice is False :
    
        choice = raw_input(msg+'>')    
        
        try:
            choice = int(choice)#int raises an exception if number is like 11.2
            if ((bottom != False) and (choice < bottom)) or ((cap != False) and (choice > cap)):
                raise 1
        except:
            choice = False
            print "Please give a valid answer."
  
    return choice

if __name__ == '__main__':
    print getChoice(14,18)

Regards,
Tudor
Reply With Quote  
Join Date: Mar 2008
Posts: 15
Reputation: rikxik is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
rikxik rikxik is offline Offline
Newbie Poster

Re: Can someone help me improve this function?

  #3  
May 23rd, 2008
Nothing radical - basically worked on your original code:

  1. def getChoice(bottom=False, cap=False):
  2. while True:
  3. try:
  4. choice = int(raw_input("Please enter your choice: "))
  5. if ((bottom != False) and (choice <= bottom)):
  6. print "Choice should be greater than", bottom
  7. continue
  8. if ((cap != False) and (choice >= cap)):
  9. print "Choice should be less than", cap
  10. continue
  11. return choice
  12. except ValueError:
  13. print "Please input intger."
Last edited by rikxik : May 23rd, 2008 at 5:07 am.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Python Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Python Forum

All times are GMT -4. The time now is 7:13 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC