具有多个字符串条件的 While 循环不能离开循环

While Loop with Multiple String Conditions Cannot Leave Loop

我是 Python 的新手,我正在做一些项目来尝试学习它。我正在尝试制作剪刀石头布游戏来学习条件和循环。我的游戏中有一个名为“验证器”的函数,它有一个带有 4 个字符串条件的 while 循环,但是当我尝试重新分配该变量以打破循环时,它仍然给我我内置的错误消息,说“请输入有效的响应".

如何让 while 循环结束并return 变量 ans?

感谢您的帮助,

-乌鸦

# Rock Paper Scissors Game
import random

# Makes the Player Choose a Valid Answer Function
def validator():
    ans = input()
    while ans != "Rock" or "Paper" or "Scissors" or "QUIT":
        ans = input("Please enter a valid response: ")
    else:
        return ans

# Comp Choosers Function

def compcho():
    v = random.randrange(1, 3)
    if v == 1:
        return "Rock"
    if v == 2:
        return "Paper"
    if v == 3:
        return "Scissors"


# Win decider
def decider(man, pc):
    if man == pc:
        return "It's a tie! " + man + " to " + pc + "!"
    elif man != pc:
        if man == "Rock" and pc == "Scissors":
            return "The player's choice of " + man + " beats the PC's choice of " + pc + "! Player Victory!"
        if man == "Rock" and pc == "Paper":
            return "The player's choice of " + man + " loses to the PC's choice of " + pc + ". Player Defeat."
        if man == "Scissors" and pc == "Rock":
            return "The player's choice of " + man + " loses to the PC's choice of " + pc + ". Player Defeat."
        if man == "Scissors" and pc == "Paper":
            return "The player's choice of " + man + " beats the PC's choice of " + pc + "! Player Victory!"
        if man == "Paper" and pc == "Rock":
            return "The player's choice of " + man + " beats the PC's choice of " + pc + "! Player Victory!"
        if man == "Paper" and pc == "Scissors":
            return "The player's choice of " + man + " loses to the PC's choice of " + pc + ". Player Defeat."
        if man == "Quit":
            print("Goodbye")
        else:
            print("Hmm I wasn't expecting " + man + ".")


# Program Start

print("Welcome to the Rock Paper Scissors Game!")
choice = "ham"
while choice != "Quit":

    # Chooser
    print("Please Enter Rock Paper Scissors or Quit")

    valans = validator()

    pcpick = compcho()

    print(decider(valans, pcpick))



您需要让您的函数之一 return 具有 "Quit" 的值,并用它设置变量 choice。我现在不在我的个人电脑前,所以我主要用你的代码来回答。也许稍后我会回来编辑一些更干净的东西。

# Rock Paper Scissors Game
import random

# Makes the Player Choose a Valid Answer Function
def validator():
    ans = input()
    while ans != "Rock" or "Paper" or "Scissors" or "QUIT":
        ans = input("Please enter a valid response: ")
    else:
        return ans

# Comp Choosers Function

def compcho():
    v = random.randrange(1, 3)
    if v == 1:
        return "Rock"
    if v == 2:
        return "Paper"
    if v == 3:
        return "Scissors"


# Win decider
def decider(man, pc):
    if man == pc:
        return "It's a tie! " + man + " to " + pc + "!"
    elif man != pc:
        if man == "Rock" and pc == "Scissors":
            return "The player's choice of " + man + " beats the PC's choice of " + pc + "! Player Victory!"
        if man == "Rock" and pc == "Paper":
            return "The player's choice of " + man + " loses to the PC's choice of " + pc + ". Player Defeat."
        if man == "Scissors" and pc == "Rock":
            return "The player's choice of " + man + " loses to the PC's choice of " + pc + ". Player Defeat."
        if man == "Scissors" and pc == "Paper":
            return "The player's choice of " + man + " beats the PC's choice of " + pc + "! Player Victory!"
        if man == "Paper" and pc == "Rock":
            return "The player's choice of " + man + " beats the PC's choice of " + pc + "! Player Victory!"
        if man == "Paper" and pc == "Scissors":
            return "The player's choice of " + man + " loses to the PC's choice of " + pc + ". Player Defeat."
        if man == "Quit":
            print("Goodbye")
            # Must return a value here... 
            return "Quit"
        else:
            print("Hmm I wasn't expecting " + man + ".")
            # doesn't matter what gets returned here based on your other code so lets return "Keep Playing" for consistency 
            return "Keep Playing"


# Program Start

print("Welcome to the Rock Paper Scissors Game!")
choice = "ham"
while choice != "Quit":

    # Chooser
    print("Please Enter Rock Paper Scissors or Quit")

    valans = validator()

    pcpick = compcho()
 
    choice = decider(valans, pcpick) # see we set choice to the value of the decided function. 
    print(choice)
    # now the loop will have a condition that will cause it to exit.

欢迎来到 SO!写了一个格式正确的问题做得很好。

您在这里遇到的问题与条件表达式的计算方式有关。你现在的方式,Python 是这样做的:

while (ans != "Rock") or ("Paper") or ("Scissors") or ("QUIT")

在这种情况下,字符串“Paper”、“Scissors”和“Quit”本身的计算结果始终为真。你可以在这里看到:

>>> bool("Rock")
True

你要的是:

while ans not in ["Rock", "Paper", "Scissors", "QUIT"]:

你可以这样写你的条件(重复变量名并检查你的逻辑运算符):

while ans != "Rock" and ans != "Paper" and ans != "Scissors" and ans != "QUIT":

或使用'in':

while ans not in ["Rock","Paper","Scissors","QUIT"] :