•
•
•
•
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
![]() |
•
•
Join Date: Apr 2008
Posts: 26
Reputation:
Rep Power: 1
Solved Threads: 0
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:
I already have a bit of code for this, but I hate its sheer ugliness and clumsiness. Here it is:
I'm not even sure if it's bug-free. Anyone able to suggest improvements or better implementations?
- 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:
Python Syntax (Toggle Plain Text)
def getChoice(bottom,cap): testOne = False testTwo = False while testOne == False or testTwo == False: choice = raw_input() try: choice = int(choice) testOne = True except: print "Please give a valid answer." testOne = False if (testOne == True): if ((choice < bottom) and (bottom != False)) or ((choice > cap) and (cap != False)) or (choice != round(choice)): print "Please give a valid answer." testTwo = False else: testTwo = True return choice
I'm not even sure if it's bug-free. Anyone able to suggest improvements or better implementations?
•
•
Join Date: May 2008
Posts: 3
Reputation:
Rep Power: 0
Solved Threads: 0
Hi,
I'm just starting with Python, so I took your question like an exercise...here is what I did:
Regards,
Tudor
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
•
•
Join Date: Mar 2008
Posts: 15
Reputation:
Rep Power: 1
Solved Threads: 1
Nothing radical - basically worked on your original code:
python Syntax (Toggle Plain Text)
def getChoice(bottom=False, cap=False): while True: try: choice = int(raw_input("Please enter your choice: ")) if ((bottom != False) and (choice <= bottom)): print "Choice should be greater than", bottom continue if ((cap != False) and (choice >= cap)): print "Choice should be less than", cap continue return choice except ValueError: print "Please input intger."
Last edited by rikxik : May 23rd, 2008 at 5:07 am.
![]() |
•
•
•
•
•
•
•
•
DaniWeb Python Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- improve performance of the following io codes (C)
- A function which will transfer the data from a file to STL( vector or map) object. (C++)
- improve events speed (C#)
- How to improve program with string compairing? (C++)
Other Threads in the Python Forum
- Previous Thread: Crazy question -- Python + Command Line
- Next Thread: simple graphics


Linear Mode