2014-02-21 56 views
1

我一直在試圖編寫一個簡單的RPS遊戲,其中用戶扮演計算機。 在這一點上只有1次迭代(因爲我被卡住了......),並且沒有驗證用戶的輸入是否有效。 我遇到的問題是,無論我爲了獲得計算機和用戶的分數而存儲在一個變量中的操作,它都保持等於0,無論我嘗試了什麼縮進或者試圖放置哪個位置返回。 (正如你可以在下面的代碼中看到的那樣...我嘗試着沒有運氣的一些部分被註釋掉了)。理想情況下,因爲我計劃進行多次迭代,所以我希望變量u和v本身被更新並保持得分,而不僅僅是打印贏得特定迭代的人。Python新手練習:岩石剪刀

你能幫我理解我在這裏做錯了嗎?

乾杯

from random import randrange 
rps = ["r", "p", "s"] 



def gameplay(user,comp): 
    u = 0 #user's score 
    v = 0 #computer's score 
    print "Computer chose: " + comp 
    if comp == user: 
     pass 
    elif comp == "r" and user == "p": 
     u += 1 
     #print u 
     #return u 

    elif comp == "r" and user == "s": 
     v += 1 
     #print v 
     #return v 

    elif comp == "p" and user == "s": 
     u += 1 
     #print u 
     #return u 

    elif comp == "p" and user == "r": 
     v += 1 
     #print v 
     #return v 

    elif comp == "s" and user == "r": 
     u += 1 
     #print u 
     #return u 

    elif comp == "s" and user == "p": 
     v += 1 
     #print v 
     #return v 

    return u 
    return v 

gameplay(raw_input("your move?"),rps[randrange(3)]) 
print u 
print v 
+1

嘗試'返回U,v'和'U,v =遊戲(...)' –

+0

當函數返回值,函數完成,所以你永遠不會返回v。你可以在一個語句ret中返回它們urn(u,v)。之後返回值必須存儲在變量中:u,v =遊戲玩法(...) –

回答

1

有兩個問題與您的程序:

  1. 函數可以返回多個值,但它必須返回他們作爲一個元組,而不是首先一個,那麼其他。將return語句更改爲return (u, v)
  2. 您需要將函數返回的值綁定到函數外的某些值,以便您可以使用它們。嘗試u, v = gameplay(...)。這會將從函數返回的uv的值綁定到函數外部的變量uv。變量的名稱不必匹配;你可以使用任何其他名稱,如my_score, cpu_score = gameplay(...)

當這兩個是固定的,它似乎工作正常。

1

每次你所說的「遊戲」伊始要設置U,V = 0,0。你需要定義的方法外得分,使一切正常工作時間:

from random import randrange 
rps = ["r", "p", "s"] 

u, v = 0, 0 

def gameplay(user,comp,u,v): 
    print "Computer chose: " + comp 
    if comp == user: 
     pass 
    elif comp == "r" and user == "p": 
     u += 1 
    elif comp == "r" and user == "s": 
     v += 1 
    elif comp == "p" and user == "s": 
     u += 1 
    elif comp == "p" and user == "r": 
     v += 1 
    elif comp == "s" and user == "r": 
     u += 1 
    elif comp == "s" and user == "p": 
     v += 1 
    return u, v 

for i in range(4): # 4 Games 
    u, v = gameplay(raw_input("your move?"),rps[randrange(3)],u,v) 

print "User: ", u, " CPU: ",v #Displaying final scores 
+0

感謝此。當我試圖拿u,v在函數外部解釋器產生一個錯誤:UnboundLocalError:在分配之前引用的局部變量'u' – Optimesh

+0

編輯:好吧,我看到你在那裏做了什麼。我嘗試添加 ,同時u <2和v <2: u_score,v_score =遊戲玩法(raw_input 後功能,但它不會'保持分數' – Optimesh

+0

它的產生是因爲在「遊戲」你沒有定義任何(u,v)這就是爲什麼我的答案(u,v)是參數,因此該方法可以知道什麼他們正確地使用它們。 ;) – CCP

2

只是爲了好玩......看看你能理解這個OOP的實現(它從來都不會太早開始學習OOP)

WIN,TIE,LOSE = 1,0,-1 
class RockPaperScissors: 
    def __str__(self): 
     return self.__class__.__name__ 
class Rock(RockPaperScissors): 
    def __cmp__(self,other): 
     if isinstance(other,Rock): return TIE 
     if isinstance(other,Paper): return LOSE 
     return WIN 

class Paper(RockPaperScissors): 
    def __cmp__(self,other): 
     if isinstance(other,Paper): return TIE 
     if isinstance(other,Scissors):return LOSE 
     return WIN 

class Scissors(RockPaperScissors): 
    def __cmp__(self,other): 
     if isinstance(other,Scissors): return TIE 
     if isinstance(other,Rock):return LOSE 
     return WIN 

def RPS(ltr_code): 
    return {'r':Rock,'p':Paper,'s':Scissors}[ltr_code.lower()]() 


player = RPS("r") 
computer = RPS("s") 
if player > computer: 
    print "Player Wins! %s beats %s"%(player,computer) 
elif player < computer: 
    print "Computer wins!! %s beats %s"%(computer,player) 
else: 
    print "Tied!! %s ties %s"%(computer,player) 
+0

擾流板:nope。:)還沒有學過__str__,__cmp__呢但是謝謝 - 會回來的 – Optimesh