Reputation: 207
So I'm programming a litlle game and I'm trying to do something which I don't understand how to do. I've defined a function, and when none of the conditions with which the code works I want it to go back to an other line of code. But I don't understand how to. THis is a part of the code i"m working on
print "What's your favourite type of pokemon?"
fav_type = raw_input()
def chosen_pokemon():
if "fire" in fav_type:
print "So you like fire types? I'll give you a chimchar!"
elif "water" in fav_type:
print "So you like water types? I'll give you a piplup!"
elif "grass" in fav_type:
print "So you like grass types? I'll give you a turtwig!"
else:
print "sorry, you can only choose grass, water or fire"
start()
chosen_pokemon()
like I want the code to go back to "What's your favourite type of pokemon?" if it ends up by else, but how? Please be as clear as possible when answering because I've just started to learn programming and I know almost nothing about it
Upvotes: 2
Views: 35571
Reputation: 595
I am a brand new in Python. Instead of using loop, I used recursion to solve this problem by calling chosen_pokemon() itself in block of else statement
def chosen_pokemon():
print "What's your favourite type of pokemon?"
fav_type = raw_input()
if "fire" in fav_type:
print "So you like fire types? I'll give you a chimchar!"
elif "water" in fav_type:
print "So you like water types? I'll give you a piplup!"
elif "grass" in fav_type:
print "So you like grass types? I'll give you a turtwig!"
else:
print "sorry, you can only choose grass, water or fire. Try again"
chosen_pokemon()
chosen_pokemon()
I hope this solution going to help you guys.
Upvotes: 1
Reputation: 10995
def chosen_pokemon(inp):
if "fire" in inp:
print "So you like fire types? I'll give you a chimchar!"
return True
elif "water" in inp:
print "So you like water types? I'll give you a piplup!"
return True
elif "grass" in inp:
print "So you like grass types? I'll give you a turtwig!"
return True
else:
print "sorry, you can only choose grass, water or fire"
return False
print "What's your favourite type of pokemon?"
while(True):
if(chosen_pokemon(raw_input())):
break
Edit: You can also put your responses in a dictionary to avoid long if-else-clauses:
print "What's your favourite type of pokemon?"
dic = {"fire":"So you like fire types? I'll give you a chimchar!", \
"water":"So you like water types? I'll give you a piplup!", \
"grass":"So you like grass types? I'll give you a turtwig!"}
while(True):
d = raw_input()
if d in dic:
print dic[d]
break
else:
print "sorry, you can only choose grass, water or fire"
Upvotes: 2
Reputation: 18841
You can bring the raw_input
step into a while loop and only exit if a valid response was received. I added a couple of comments to explain what's going on.
def chosen_pokemon():
while True: # repeat forever unless it reaches "break" or "return"
print "What's your favourite type of pokemon?"
fav_type = raw_input()
if "fire" in fav_type:
print "So you like fire types? I'll give you a chimchar!"
elif "water" in fav_type:
print "So you like water types? I'll give you a piplup!"
elif "grass" in fav_type:
print "So you like grass types? I'll give you a turtwig!"
else:
print "sorry, you can only choose grass, water or fire"
continue # jumps back to the "while True:" line
return # finished; exit the function.
chosen_pokemon()
Output:
>>> chosen_pokemon()
What's your favourite type of pokemon?
yellow
sorry, you can only choose grass, water or fire
What's your favourite type of pokemon?
grass
So you like grass types? I'll give you a turtwig!
>>>
And here's a tutorial on while
loops. http://www.tutorialspoint.com/python/python_while_loop.htm
Upvotes: 4
Reputation: 2504
You just need to put what you have into another loop that returns to the top and asks the question again until a valid response is received.
Upvotes: 0