我正在爲一個班級創建一個Rock,Paper,Scissors遊戲。作爲遊戲的一部分,我需要在屏幕上顯示一個武器菜單顯示供用戶選擇。然後電腦會從列表中隨機選擇一件武器。我面臨的問題(我相信)是列表項從[0,2]我的菜單項列表[1,3]。我搜索了幾個小時,但我不明白我一直在網上閱讀的複雜的東西,所以我不知道如何應用它們。將用戶輸入與隨機選擇的列表項目進行比較 - Python
# random integer
from random import randint
# list for weapon
WEAPON = ["Rock", "Paper", "Scissors"]
# one player mode
def onePlayer():
scoreP = 0
scoreC = 0
again = ""
player = False
print("---------------------------------------------")
print("\n\tPlayer VS Computer")
while player == False:
print("Weapons:")
print("1. Rock")
print("2. Paper")
print("3. Scissors")
print("4. Quit")
player = input("\nSelect your weapon: ")
if player == "quit" or player == "q" or player == "4":
player = True
main()
else:
try:
player = int(player)
if player == 1:
player = WEAPON[0]
elif player == 2:
player = WEAPON[1]
elif player == 3:
player = WEAPON[2]
except:
print("please enter a number 1 through 4\n")
computer = WEAPON[randint(0,2)]
if player == computer:
print(player," vs ",computer)
print("It's a tie!\n")
print("Player:",scoreP,"\nComputer:",scoreC)
print("")
elif player == 1:
# computer == paper
if computer == 1:
print(player," vs ",computer)
print("Paper covers rock! You lose!\n")
scoreC = scoreC + 1
print("Player:",scoreP,"\nComputer:",scoreC)
print("")
else:
print("Rock smashes scissors. You win!\n")
scoreP = scoreP + 1
print("Player:",scoreP,"\nComputer:",scoreC)
print("")
elif player == 2:
if computer == 2:
print(player," vs ",computer)
print("Scissors cut paper! You lose!\n")
scoreC = scoreC + 1
print("Player:",scoreP,"\nComputer:",scoreC)
print("")
else:
print("Paper covers rock. You win!\n")
scoreP = scoreP + 1
print("Player:",scoreP,"\nComputer:",scoreC)
print("")
elif player == 3:
if computer == 0:
print(player," vs ",computer)
print("Rock smashes scissors! You lose!\n")
scoreC = scoreC + 1
print("Player:",scoreP,"\nComputer:",scoreC)
print("")
else:
print("Scissors cut paper. You win!\n")
scoreP = scoreP + 1
print("Player:",scoreP,"\nComputer:",scoreC)
print("")
#else:
# print("Please select a valid play option\n")
player = False
請不要介意if/else語句中的打印語句。我意識到這些將需要改變。我的主要問題是將用戶輸入與計算機的隨機列表選擇進行比較的邏輯。
代碼有什麼問題?你能把它變得更簡潔嗎? (這是什麼問題?) – Jerrybibo