随机化器游戏 - 两个语义错误

时间:2014-10-02 05:42:42

标签: python

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循环有关。

这两个错误都可以在这里看到: image link

2 个答案:

答案 0 :(得分:0)

我想你要改变

while player1 >= 0 and player2 >= 0:

while player1 > 0 and player2 > 0:

因为如果player1 == 0或者player2 == 0,你希望循环停止。

答案 1 :(得分:0)

我认为使用条件

while player1 > 0 and player2 > 0:
当任一玩家获胜时,

将帮助您退出。此外,您可以在增加/减少其值后​​显示每个玩家的硬币数。因此,当计数减少时,它将显示0,在这种情况下条件满足并且您将退出循环。在这个时间点,不应再有轮次。