# How to make function more pythonic?

Is there a way to accomplish this function in less lines of code? I am learning python and want to make sure I am baking in good habits...I repeat myself twice which I don't like and it seems "european in answer or african in answer" is a sloppy way to search for those two strings.

def door():
print "Welcome to the door!"
print "A man has been waiting for you. To enter you must get his question correct."
print "What is the average velocity of a barn swallow?"

print "Welcome inside!"
else:
i = 3
while i != 0:
print "You have %d guesses left." % i
i -= 1

print "Welcome inside!"
break
elif i == 0:
print "How do you not ask what kind of swallow?!"
else:
continue

-

## migrated from stackoverflow.comJan 30 '13 at 23:13

This question came from our site for professional and enthusiast programmers.

The final else: continue in your code doesn't do anything and could be removed. It is also fairly simple to remove some repetition just by moving the bit that gets the input and responds for the first time, to the inside of the loop. And a for...else loop may be preferable to while here.

def door():
print """Welcome to the door!
A man has been waiting for you. To enter you must get his question correct.
What is the average velocity of a barn swallow?"""
for attempt in reversed(range(4)):
print "Welcome inside!"
break
if attempt:
print "You have %d guesses left." % attempt
else:
print "How do you not ask what kind of swallow?"


I don't see anything wrong with "european" in answer or "african" in answer, but if you had a larger number of permissible answer strings you could do something like:

swallow_types = 'african', 'asian', 'european'
if any(swallow in answer for swallow in swallow_types):


If you like recursion you could do...

def door():
return "Welcome inside"
if left:
print "You have %d guesses left." % left
print """Welcome to the door!
A man has been waiting for you. To enter you must get his question correct.
What is the average velocity of a barn swallow?"""
print get_answer(left=3) or "How do you not ask what kind of swallow?"


(Though this is not particularly recommended because it adds needless complexity - see comments. I leave it in just to show another way that you could do it.)

As general advice, not repeating yourself, reducing lines of code and making code more pythonic are not always the same thing. The ideas here are not that much more pythonic than your original, but just avoid repetition by altering the structure. Pythonic is not always brief in terms of number of lines because of the guiding principles 'explicit is better than implicit', 'sparse is better than dense', and 'readability counts'.

-
I wouldn't use recursion here, there's no good reason to do so. A normal loop would (should) be preferable in this case. – Jeff Mercado Jan 31 '13 at 2:18
@JeffMercado I agree but will leave it in there just to show it's another possibility. – Stuart Jan 31 '13 at 2:24
No need for complexity at swallow types. Just do: if answer in ['african', 'european', ...] – user1882585 Jan 31 '13 at 9:44
@user1882585 No, that is different from the original post, which searches for a string within the answer, while your idea searches for the answer within a list of strings. For instance "Do you mean an african or european swallow?" would be a valid answer in the original code. – Stuart Jan 31 '13 at 11:24