2013-10-28 39 views
0

在下面的岩石剪刀程序中,我的循環出了問題。使用輸入設定長度,遊戲每次播放N次。 對於結果爲玩家2的情況,計算機0;玩家0,電腦2;玩家2,電腦5;玩家0,電腦4;一個額外的遊戲被添加到該集。我已經多次改變了這個功能,並且無法弄清楚什麼是錯誤的。岩石剪刀流量控制

def rpsls_play(): 
    print("Welcome to the Rock-Scissors-Paper-Lizard-Spock game!") 
    player_sets=0 
    N=int(input("Select set length: ")) 
    times=0 
    player_wins=0 
    computer_wins=0 
    while times < N: 
     times +=1 
     print("Now beginning game", times) 
     if rpsls_game()==1: 
      player_wins +=1 
     else: 
      computer_wins +=1 
     print("Set score: Player", str(player_wins)+", Computer", str(computer_wins)) 
    else: 
     pass 

    if player_wins==computer_wins: 
     while abs(player_wins-computer_wins)<2:   
      times +=1 
      print("Now beginning game", times) 
      if rpsls_game()==1: 
       player_wins +=1 
      else: 
       computer_wins +=1 
      print("Set score: Player", str(player_wins)+", Computer", str(computer_wins)) 
    else: 
     pass 

    if player_wins>computer_wins: 
     print("Congratulations! You have won in", times, "games.") 
     player_sets +=1 
    elif computer_wins>player_wins: 
     print("Too bad! You have lost in", times, "games.") 

    pass 

感謝您的幫助

回答

1

我無法檢查你的代碼,因爲沒有功能rpsls_game()

你沒有說你的遊戲有什麼問題 - 你會得到什麼,你期望什麼?

我只能猜測,你有行

if player_wins==computer_wins: 

額外的遊戲在N場比賽只有player_wins == computer_wins添加問題。

你不需要那條線。

+0

我需要的線路,因爲如果出現平局,他們必須打,直到一個由兩輪問題的勝率,由於某種原因遊戲正在當不存在聯繫時添加 – user2906979

+0

英語不是我的母語,我認爲你不僅需要'player_wins == computer_wins'而且還需要'abs(player_wins-computer_wins)<2'的額外遊戲。我用'random'測試'rpsls_game()'來測試你的代碼(python 2.x,3.x),而且我沒有看到問題。這個對我有用。它只有在'player_wins == computer_wins'時纔會增加額外的遊戲。 – furas

+0

也許你在一個文件中進行了修改,但是不小心你用舊代碼運行了不同的文件。 – furas

1

夫婦代碼設計的東西,這將有助於提高代碼之前,我得到了答案:

  1. 您可以刪除這些else: pass線。

  2. 您可以刪除最後的pass聲明。

您需要將其更改爲:

if player_wins == computer_wins:   
    times +=1 
    print("Now beginning tie-breaker game. game", times) 
    if rpsls_game()==1: 
     player_wins +=1 
    else: 
     computer_wins +=1 
    print("Set score: Player", str(player_wins)+", Computer", str(computer_wins))