【问题标题】:While Loop with Multiple String Conditions Cannot Leave Loop具有多个字符串条件的while循环不能离开循环
【发布时间】:2020-12-30 02:57:42
【问题描述】:

我是 Python 新手,我正在做一些项目来尝试学习它。我正在尝试制作一个石头剪刀布游戏来学习条件和循环。我的游戏中有一个名为“validator”的函数,它有一个带有 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-loop multiple-conditions


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

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

      或使用'in':

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

      【讨论】:

      • 我使用了您的第一个解决方案,它奏效了!非常感谢!
      【解决方案3】:

      欢迎来到 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"]:
      

      【讨论】:

      • 谢谢!我不知道这是写出那个条件的一种方式。
      猜你喜欢
      • 1970-01-01
      • 2017-05-03
      • 1970-01-01
      • 2013-07-06
      • 1970-01-01
      • 2022-11-04
      • 2013-09-29
      • 2013-08-07
      • 1970-01-01
      相关资源
      最近更新 更多