我正在嘗試構(gòu)建一個石頭、紙、剪刀游戲。我似乎無法讓代碼進行鍛煉,因為無論我做出什么選擇,贏家總是電腦!我的電腦有偏見嗎?感謝您的幫助 !import randomcomp_list = ['Rock', 'Paper', 'Scissors']computer = c = 0command = p = 0print("Scores: Computer " + str(c) + " player " + str(p))while True: comp = random.choice(comp_list) command = input("rock , paper, scissors or quit :").lower() if command == comp : print("break even") elif command == "rock": if comp == "scissors": print("player won") p += 1 else: print("computer won") c +=1 elif command == "paper": if comp == "rock": print("player won") p += 1 else : print("computer won") c += 1 elif command == "scissors": if comp == "paper": print("player won") p += 1 else : print("computer won") c += 1 elif command == "quit": break else : print("wrong command! ") print("Player: " + command) print("computer: " + comp) print("") print("Scores: Computer " + str(c) + " player " + str(p)) print("")
1 回答
蝴蝶刀刀
TA貢獻1801條經(jīng)驗 獲得超8個贊
您的計算機總是生成一個大寫字符串 ex
Rock
你的輸入總是小寫的
rock
在 python 字符串情況下很重要,所以當你比較時
if command == comp:
這是比較
"Rock" == "rock" # -> which is false
您可以通過對字符串使用全部小寫來修復(fù)您的代碼。只需將頂部的數(shù)組更改為全部小寫即可
['rock', 'paper', 'scissors']
添加回答
舉報
0/150
提交
取消
