2014-10-02 19 views
-1
import random 
print "This is a Coin War!" 
coins_each = raw_input("How many coins for each player?:") 
coins_each = int(coins_each) 
player1 = coins_each 
player2 = coins_each 
possibilities = ["Heads", "Tails"] 
count = 1 
while player1 >= 0 and player2 >= 0: 
    print "Round", count, "Player A:", player1, "Player B:", player2 
    coin1 = random.choice(possibilities) 
    coin2 = random.choice(possibilities) 
    count = count + 1 
    print "\tCoin A:", coin1 
    print "\tCoin B:", coin2 
    if coin1 == coin2: 
     player1 = player1+1 
     player2 =player2-1 
     print "Same ---> A wins" 
     print ("") 
    else: 
     player1 = player1-1 
     player2= player2+1 
     print "Different--->B wins", 
     print ("") 
    if player1 == 0: 
     print "Player B wins the game" 
    if player2 == 0: 
     print "Player A wins the game" 

現在,我遇到了兩個障礙。 「玩家A贏得比賽」是在最後一輪比賽前進行打印。有時候,遊戲不會在零點擊中時結束 - 我相信這與我的while循環有關。Randomizer遊戲 - 兩個語義錯誤

這兩個錯誤可以在這裏看到: image link

回答

0

我想你想改變

while player1 >= 0 and player2 >= 0: 

while player1 > 0 and player2 > 0: 

,因爲你希望循環停止,如果任一PLAYER1 = = 0或player2 == 0.

+0

我試過了,它會導致http://i.imgur.com/YmjJf2A.png上一輪不打印。 – aero26 2014-10-02 05:52:40

+0

您可以打印循環後的最後一個循環,即在循環退出後,在while循環的頂部添加print語句。 – mwm314 2014-10-02 05:57:33

+0

好的。給那個試一試。 http://i.imgur.com/Ky9IVk2.png上一輪的印刷,似乎總是重印前一輪的硬幣。 – aero26 2014-10-02 06:13:32

0

我認爲使用條件

while player1 > 0 and player2 > 0: 

將幫助你退出,當任何一方獲勝時。您也可以在增加/減少數值後顯示每位玩家的硬幣數量。所以當計數減少時,它會顯示0,在這種情況下條件滿足,您將退出循環。此時此刻不應再有輪迴。