栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

循环回到代码中的特定点

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

循环回到代码中的特定点

您可以使该

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



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/516982.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号