我想在我的代码中有一个是/否循环,但我在这样做时遇到了麻烦(python 3.3)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22362165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:48:22  来源:igfitidea点击:

I want to have a yes/no loop in my code, but I'm having trouble doing it (python 3.3)

pythonloops

提问by user3412375

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)

抱歉,我只是 python 的初学者,所以这可能是一个非常简单的问题,但是我有一个代码,我想循环它,以便在代码询问用户是否要再次播放并且用户输入“是”之后重新启动代码并“否”结束代码。如果他们输入的是或否以外的任何内容,它应该要求告诉他们输入是或否,然后再次提问。我将如何做到这一点?(我确实知道 while 和 for 循环,但我不确定如何以这种方式使用它们)

回答by Aswin Murugesh

This is a simple one:

这是一个简单的:

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

其中gameplay函数包含要执行的代码

回答by halex

I would do it the following way:

我会这样做:

while True:
    # your code
    cont = raw_input("Another one? yes/no > ")
    while cont.lower() not in ("yes","no"):
        cont = raw_input("Another one? yes/no > ")
    if cont == "no":
        break

If you use Python3 change raw_inputto input.

如果您使用 Python3,请更改raw_inputinput.

回答by Signus

My approach to this:

我的方法是:

# Sets to simplify if/else in determining correct answers.
yesChoice = ['yes', 'y']
noChoice = ['no', 'n']

# Prompt the user with a message and get their input.
# Convert their input to lowercase.
input = raw_input("Would you like to play again? (y/N) ").lower()

# Check if our answer is in one of two sets.
if input in yesChoice:
    # call method
elif input in noChoice:
    # exit game
    exit 0
else: 
    print "Invalid input.\nExiting."
    exit 1

回答by user3330510

I think this is what you are looking for

我想这就是你要找的

def playGame():
    # your code to play

if __name__ == '__main__':
    play_again = 'start_string'
    while not play_again in ['yes', 'no']:
        play_again = raw_input('Play Again? (type yes or no) ')
    if play_again == 'yes':
        playGame()

回答by Juan Martinez

I tried this short boolean script that will maintain the loop until the if statement is satisfied:

我尝试了这个简短的布尔脚本,它将保持循环直到满足 if 语句:

 something = False
    while not something:
    inout = raw_input('type "Hello" to break the loop: ')
    if inout == 'Hello':
        something = True