我目前正在用Python編寫一個小小的搖滾,紙張,剪刀遊戲,但是我似乎偶然發現了一個問題。儘管代碼有點粗糙,但遊戲仍然有效,但我試圖讓程序通知玩家他們犯了一個錯誤,當我測試這個時,它隨機通知玩家他們犯了一個錯誤,當他們還沒有。這是我的問題的代碼塊,它不是整個遊戲。Python中的岩石,紙張,剪刀遊戲的問題
def game(self):
print "This is rock, paper, scissors!"
rps = ('rock', 'paper', 'scissors')
comp1 = raw_input("Rock, paper or scissors?\n> ")
comp2 = random.choice(rps)
if comp1 == 'rock' and comp2 == 'scissors' or comp1 == 'scissors' and comp2 == 'paper' or comp1 == 'paper' and comp2 == 'rock':
print "You won! The computer chose %s" % comp2
return "game"
elif comp1 == 'rock' and comp2 == 'rock':
print "In a stalemate, there are no winners, only losers :)\nThe computer also chose %s" % comp2
return "game"
elif comp1 == 'scissors' and comp2 == 'scissors':
print "In a stalemate, there are no winners, only losers :)\nThe computer also chose %s" % comp2
return "game"
elif comp1 == 'paper' and comp2 == 'paper':
print "In a stalemate, there are no winners, only losers :)\nThe computer also chose %s" % comp2
return "game"
elif comp1 != 'rock' or 'scissors' or 'paper':
print "Try choosing between rock, paper and scissors next time. It might help.
return "game"
else:
print "The computer %s. You have failed. Problem?" % comp2
return "game"
在我的手機上,但試着將COMP1轉換爲小寫。您也可以通過comp1 == comp2來簡化邏輯,以達到僵局 – billinkc
您沒有複製真實代碼,因爲您複製的內容具有無效的字符串文字。 –
爲什麼你總是返回字符串「遊戲」,這似乎很奇怪......可能與你的問題無關。 –