gpt4 book ai didi

python - 我的 "rock, paper,scissors"代码有问题

转载 作者:太空宇宙 更新时间:2023-11-03 15:34:58 26 4
gpt4 key购买 nike

嘿伙计们,我已经编写了下面的代码,一切似乎都工作正常,除了当它应该是平局时我不断得到“name2 wins”以及当答案应该无效时。我不知道出了什么问题,因为我的逻辑似乎是正确的。

print("Welcome to Rock, Paper, Scissors!")
print("Let's Begin ...")
name1 = input("Player 1: What's your name?")
name2 = input("Player 2: What's your name?")

print("Hello " + name1 + " and " + name2)
print(name2 + ": Close your eyes!")

choice1 = input(name1 + ": enter 'r' for rock, 'p' for paper, and 's' for Scissors: ")

print("Great choice! Now - cover your answer and ask " + name2 + " to choose. \n\n\n")
choice2 = input(name2 + ": enter 'r' for rock, 'p' for paper, and 's' for scissors: ")

if(choice1 == "r" , choice2 == "p"):
print(name2 + " wins!")
elif(choice1 == "r" , choice2 == "s"):
print(name1 + " wins!")
elif(choice1 == "r" , choice2 == "r"):
print("It is a draw")
elif(choice2 == "r" , choice1 == "p"):
print(name1 + " wins!")
elif(choice2 == "r" , choice1 == "s"):
print(name2 + " wins!")
elif(choice1 == "p" , choice2 == "s"):
print(name2 + " wins!")
elif(choice1 == "p" , choice2 == "p"):
print("It is a draw!")
elif(choice2 == "p" , choice1 == "s"):
print(name1 + " wins!")
elif(choice1 == "s" , choice2 == "s"):
print("It is a draw!")
else:
print("Invalid asnwer")


print("Thanks for playing Rock, Paper, scissors")

最佳答案

您检查多个条件的整个方法不正确 -

if(choice1 == "r" , choice2 == "p"):

采用由 choice1 == "r", choice2 == "p" 形成的元组的真实性,它始终是真实的,因为它不应该为空,所以你永远不会达到 elif 语句,玩家 2 将永远获胜。

检查这两个条件的正确方法是使用 and 运算符,并且括号不是必需的。

if choice == "r" and choice2 == "p":

解决所有这些问题应该会有所帮助。

关于python - 我的 "rock, paper,scissors"代码有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42603485/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com