我按照指南制作了一个石头剪刀布游戏。一切都很顺利,但教程中没有包含平局的方法。我以为我自己可以做到这一点,但我编写的代码不起作用。我对编码非常陌生,所以我无法意识到我做错了什么。
我认为这段代码可以工作,但它只是打印玩家输给计算机时的文本。这是代码。
import random
uw = 0
cpw = 0
options = ["rock", "paper", "scissors"]
while True:
uinput = input("Type Rock/Paper/Scissors or Q to quit: ").lower()
if uinput == "q":
break
if uinput not in options:
continue
rn = random.randint(0, 2)
#rock: 0, paper: 1, scissors: 2
cpp = options[rn]
print("Computer picked", cpp + ".")
if uinput == "rock" and cpp == "scissors":
print("You won!")
uw += 1
elif uinput == "paper" and cpp == "rock":
print("You won!")`
uw += 1
elif uinput == "scissors" and cpp == "paper":
print("You won!")
uw += 1
elif uinput "paper" and cpp == "paper":
print("It's a tie, no points.")
elif uinput "rock" and cpp == "rock":
print("It's a tie, no points.")
elif uinput "scissors" and cpp == "scissors":
print("It's a tie, no points.")
else:
print("You lost!")
cpw += 1
print("You won", uw, "times.")
print("The computer won", cpw, "times")
print("Goodbye")
您没有将领带行与字符串进行比较:
elif uinput "paper" and cpp == "paper":
print("It's a tie, no points.")
应该是:
elif uinput == "paper" and cpp == "paper":
print("It's a tie, no points.")
这里,您的代码中缺少
==
,这就是检查您的
uinput
与“纸”相同