我想在我的代码中有一个是/否循环,但是这样做很麻烦(python 3.3) [英] I want to have a yes/no loop in my code, but I'm having trouble doing it (python 3.3)

查看:76
本文介绍了我想在我的代码中有一个是/否循环,但是这样做很麻烦(python 3.3)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,我只是python的初学者,所以这可能是一个非常简单的问题,但是我有一个代码,我想循环播放它,以便在代码询问用户是否要再次播放并且用户输入是"以重新启动代码,否"以结束代码.如果他们输入的是是"或否"以外的其他内容,则应要求他们输入是"或否",然后再次提出问题.我该怎么做呢?(我确实知道while和for循环,但是我不确定如何以这种方式使用它们)

Sorry I'm just a beginner at python so this is probably a very simple question, but I have a code and I want to loop it so after the code asks the user if they want to play again and the user inputs 'yes' to restart the code and 'no' to end the code. If they input anything other than yes or no it should ask tell them to enter yes or no then ask the question again. How would I do this exactly? (I do know about while and for loops but I'm not sure how I would use them in this way)

推荐答案

这是一个简单的方法:

while True:
    a = input("Enter yes/no to continue")
    if a=="yes":
        gameplay()
        continue
    elif a=="no":
        break
    else:
        print("Enter either yes/no")

其中游戏功能包含要执行的代码

Where gameplay function contains the code to be executed

这篇关于我想在我的代码中有一个是/否循环,但是这样做很麻烦(python 3.3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆