您可以使该
raw_input步骤进入while循环,并且仅在收到有效响应后才退出。我添加了一些评论来解释发生了什么。
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()
输出:
>>> chosen_pokemon()What's your favourite type of pokemon?yellowsorry, you can only choose grass, water or fireWhat's your favourite type of pokemon?grassSo you like grass types? I'll give you a turtwig!>>>
这是关于
while循环的教程。http://www.tutorialspoint.com/python/python_while_loop.htm
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)