【发布时间】: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