2011-09-10 69 views
0

我目前正在用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" 
+0

在我的手機上,但試着將COMP1轉換爲小寫。您也可以通過comp1 == comp2來簡化邏輯,以達到僵局 – billinkc

+0

您沒有複製真實代碼,因爲您複製的內容具有無效的字符串文字。 –

+0

爲什麼你總是返回字符串「遊戲」,這似乎很奇怪......可能與你的問題無關。 –

回答

9

變化

elif comp1 != 'rock' or 'scissors' or 'paper': 

elif comp1 not in rps: 

你之前在做什麼等同於:

elif (comp1 != 'rock') or 'scissors' or 'paper': 

那麼,爲什麼病情始終得到滿足?

仔細查看or 'scissors' or 'paper'部分。

1)在Python中,非空字符串被視爲True,空字符串被視爲False。看看這個交互式會話:

>>> bool('') 
False 
>>> bool('a') 
True 

2)而且在Python,if報表沒有對比(如if var1:)是,如果表達式爲true inexplicity檢查。所以,

if var1: 

相同

if var1 == True: 

如果這兩個概念結合在一起:

if 'rock': 
    # Always executed 
if '': 
    # Never executed 

回到你原來的if語句:

elif comp1 != 'rock' or 'scissors' or 'paper': 

兩個'剪刀'和'紙'將永遠返回True,所以包含的聲明將始終被評估。

那麼什麼是「in」運算符?

in的操作者在elif comp1 not in rps:將看到如果comp1內容是在元組rps(其等於('rock', 'paper', 'scissors'))的項目。它前面的not將否定它,檢查comp1的內容是否是元組rps中的項目。因此,只有當存儲在comp1中的用戶輸入無效時纔會執行包含的語句。

2

應該

comp1 not in ['rock', 'scissors', 'paper'] 

'剪刀' 和 '紙' 總是爲真(或在or情況下給自己)

comp1 != 'rock' or 'scissors' or 'paper' 

此外,使用comp1 == comp2,它更簡單。

+1

使用世界更簡單,它更簡單。 :P –

1

問題在於你的邏輯在這裏elif comp1 != 'rock' or 'scissors' or 'paper':。字符串'剪刀'和'紙'被評估爲一個布爾值,這是真的,因爲它們不是空的。

你想要的是elif comp1 != 'rock' and comp1 != 'scissors' and comp1 != 'paper':或者因爲你已經擁有它的rps元組,你可以做elif comp1 not in rps:

0

我認爲這是多一點清理的版本,雖然不是最好的實現。我還添加了一個繼續播放的選項,並對其進行了更改,以便用戶輸入不區分大小寫。

def game(): 
    import random 
    import string 
    print "This is rock, paper, scissors!" 

    rps = ('rock', 'paper', 'scissors') 

    comp1 = raw_input("Rock, paper or scissors? ") 
    comp1 = comp1.lower() 
    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 
    elif comp1 == comp2: 
     print "In a stalemate, there are no winners, only losers :)\nThe computer also chose %s" % comp2 
    elif comp1 not in rps: 
     print "Try choosing between rock, paper and scissors next time. It might help." 
    else: 
     print "The computer chose %s. You have failed. Problem?" % comp2 
    new_game = raw_input('Would you like to play again? ') 
    new_game = new_game.lower() 
    if new_game == 'yes': 
     game()