循环回到代码中的特定点

循环回到代码中的特定点,第1张

循环回到代码中的特定点

您可以使该

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



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5508202.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-13
下一篇 2022-12-13

发表评论

登录后才能评论

评论列表(0条)

保存