具有多个字符串条件的While循环无法退出循环

问题描述

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

如何使while循环结束并返回变量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))



解决方法

欢迎您!写一个结构良好的问题做得很好。

您在这里遇到的问题与条件表达式的计算方式有关。您现在拥有的方式,Python正在执行此操作:

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

在这种情况下,字符串“ Paper”,“ Scissors”和“ Quit”本身始终为true。您可以在这里看到它:

>>> bool("Rock")
True

您想要的是:

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

您需要让其中一个函数返回值"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.
,

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

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

或使用'in':

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