发行卡片时,它们不会显示为其他卡片。为什么?

时间:2018-12-03 21:55:59

标签: python python-3.x

我似乎无法让我的卡显示为其他卡。我不知道自己的代码在做什么错。

输出:

playerOne has played: 6 of Hearts | PlayerTwo has played: 9 of Clubs
PlayerTwo Wins!
playerOne has played: 6 of Hearts | PlayerTwo has played: 9 of Clubs
PlayerTwo Wins!
playerOne has played: 6 of Hearts | PlayerTwo has played: 9 of Clubs

预期输出:

playerOne has played: 7 of Hearts | PlayerTwo has played: 9 of Clubs
PlayerTwo Wins!
playerOne has played: 3 of Diamonds | PlayerTwo has played: 6 of Hearts
PlayerTwo Wins!
playerOne has played: 2 of Clubs | PlayerTwo has played: 5 of Spades

random.shuffle(deck)
for play in deck:
    firstHalf = play[0:int(52/2)]
    secondHalf = play[int(52/2):]
    for c,c2 in zip(firstHalf, secondHalf):
        a = c["value"]
        x = c["suit"]
        b = c2["value"]
        y = c2["suit"]
        print(playerOne + " has played: " + str(a) + " of " +  x + " | " + playerTwo + " has played: " + str(b) + " of " + y)
        if a > b:
            print(playerOne + " Wins! ")
        elif a < b:
            print(playerTwo + " Wins! ")
        else:
            print("This is WAR!")

3 个答案:

答案 0 :(得分:0)

您正在循环播放甲板的两半,而没有将结果保存在任何地方。因此a, x, b, y是常量,并且等于在前两个for循环中发出的最后一个“卡片”。
也许尝试“边玩边玩”:

random.shuffle(deck)
firstHalf = deck[0:int(52/2)]
secondHalf = deck[int(52/2):]
for i in range(26):
    a = firstHalf[i]["value"]
    x = firstHalf[i]["suit"]
    b = secondHalf[i]["value"]
    y = secondHalf[i]["suit"]      
    print(playerOne + " has played: " + str(a) + " of " +  x + " | " + playerTwo + " has played: " + str(b) + " of " + y)
    if a > b:
      print(playerOne + " Wins! ")
    elif a < b:
      print(playerTwo + " Wins! ")
    else:
      print("This is WAR!")

我不能保证我的代码可以运行,因为您的示例并非“可运行”,但是您的要旨是。
祝你好运!

答案 1 :(得分:0)

也许您可以尝试以下方法:

random.shuffle(deck)
for play in deck:
    firstHalf = play[0:int(52/2)]
    secondHalf = play[int(52/2):]
    for c,c2 in zip(firstHalf, secondHalf):
        a = c["value"]
        x = c["suit"]
        b = c2["value"]
        y = c2["suit"]
        print(playerOne + " has played: " + str(a) + " of " +  x + " | " + playerTwo + " has played: " + str(b) + " of " + y)
        if a > b:
            print(playerOne + " Wins! ")
        elif a < b:
            print(playerTwo + " Wins! ")
        else:
            print("This is WAR!")

不太确定要使用for play in deck做什么,但是您想在循环中更新变量,毕竟这是循环的全部要点。

答案 2 :(得分:0)

您可以zip甲板的两半并遍历它们:

random.shuffle(deck)
firstHalf = deck[0:int(52/2)]
secondHalf = deck[int(52/2):]
for d1, d2 in zip(firstHalf, secondHalf):
    v1, v2 = d1['value'], d2['value']
    print(playerOne + " has played: " + str(v1) + " of " +  d1['suit'] + " | " + playerTwo + " has played: " + str(v1) + " of " + d2['suit'])
    if v1 > v2:
      print(playerOne + " Wins! ")
    elif v1 < v2:
      print(playerTwo + " Wins! ")
    else:
      print("This is WAR!")