尝试为剪刀布石头布游戏打印"tie",但游戏总是给计算机带来胜利



在我的Python类中,我们正在制作一个Rock,Paper,Scissors游戏,但每次游戏应该打平时,它都会让计算机获胜。它显然应该打印平局,但打印电脑获胜!相反我该如何修复它?这是我的代码:

#computer choice code.  Copy and paste the code from the instructions.
import random
computerChoice = random.randint(1,3)
#player choice code
playerChoice = int(input("Please choose a number between 1 and 3: "))
#who won?
if (playerChoice == computerChoice):
result = """
_______ _      _ 
|__   __(_)    | |
| |   _  ___| |
| |  | |/ _  |
| |  | |  __/_|
|_|  |_|___(_)

"""
if (playerChoice == 1 and computerChoice == 3 or 
playerChoice == 2 and computerChoice == 1 or 
playerChoice == 3 and computerChoice == 2):
result = """
__     __          __          ___       _ 
    / /                   / (_)     | |
 _/ /__  _   _      /  / / _ _ __ | |
   / _ | | | |    /  / / | | '_ | |
| | (_) | |_| |      /  /  | | | | |_|
|_|___/ __,_|     /  /   |_|_| |_(_)

"""
else:
result = """
_____ _____  _    _  __          ___           _ 
/ ____|  __ | |  | |          / (_)         | |
| |    | |__) | |  | |     /  / / _ _ __  ___| |
| |    |  ___/| |  | |    /  / / | | '_ / __| |
| |____| |    | |__| |      /  /  | | | | __ _|
_____|_|     ____/      /  /   |_|_| |_|___(_)

"""

print(result)

原因是您的第二个"如果";(或"其他"部分(总是执行;结果";总是被"覆盖;你赢了"或";CPU获胜";。

要修复它,只需将其更改为elif

if (playerChoice == computerChoice):
result = """
_______ _      _ 
|__   __(_)    | |
| |   _  ___| |
| |  | |/ _  |
| |  | |  __/_|
|_|  |_|___(_)

"""
elif (playerChoice == 1 and computerChoice == 3 or 
playerChoice == 2 and computerChoice == 1 or 
playerChoice == 3 and computerChoice == 2):
result = """
__     __          __          ___       _ 
    / /                   / (_)     | |
 _/ /__  _   _      /  / / _ _ __ | |
   / _ | | | |    /  / / | | '_ | |
| | (_) | |_| |      /  /  | | | | |_|
|_|___/ __,_|     /  /   |_|_| |_(_)

"""
else:
result = """
_____ _____  _    _  __          ___           _ 
/ ____|  __ | |  | |          / (_)         | |
| |    | |__) | |  | |     /  / / _ _ __  ___| |
| |    |  ___/| |  | |    /  / / | | '_ / __| |
| |____| |    | |__| |      /  /  | | | | __ _|
_____|_|     ____/      /  /   |_|_| |_|___(_)

"""

print(result)

最新更新