[Tutor] (no subject)

Jose Amoreira ljmamoreira at gmail.com
Sat Jun 12 11:20:01 CEST 2010


On Friday, June 11, 2010 10:12:27 pm Advertising Department wrote:
> #!/Library/Frameworks/Python.framework/Versions/Current/bin/pythonw
> """still thinking in imperative"
> """
> 
> ## obviously this is a bad construct.
> ## Can someone suggest a pythonesque way of doing this?
> 
> 
> def getid():
> 	response  = raw_input('prompt')
> 	if response not in [ "", "y", "Y", "yes"] :
> 		getid()	# ouch
> 	print "continue working"
> 	# do more stuff
> 	# do more stuff
> 
> 
> getid()
> dosomething()
> getid()
> dosomethingelse()
> 
> 
> ## obviously this is a bad construct.
> ## Can someone give me a pythonesque way of doing this?
> 
Using recursion for validation, that doesn't sound right. I would rather do it 
with a simple while cycle:

response="any invalid string"
while response not in ["","y","Y","yes"]:
	response = raw_input("prompt")

Hope this helps
José


More information about the Tutor mailing list