如果它打成平局,我如何制作这个循环?

时间:2017-10-24 03:28:51

标签: python

我试图让我的程序循环,如果计算机和人类绘制相同的卡,这是我正在开发的战争游戏,如果两个都在同一张卡上,我希望程序循环并绘制更多的卡,直到领带破了!我把它带到了一个新的阶段,但是当我在pycharm上运行它时出错,不确定

这次看起来没问题,我改变了所有值而不是打印它现在设置了值,然后在程序中打印

import random
while True:
cardValue = random.randint(2,14)
cardSuit = random.randint (0,3)
humanCard = random.randint (2,14)
humanSuit = random.randint (0,3)


if cardValue == 14:
    print ("Ace")
elif cardValue == 13:
    print ("King")
elif cardValue == 12:
    print ("Queen")
elif cardValue == 11:
    print ("Jack")
elif cardValue < 10:
    print (cardValue)

if cardSuit == 0:
    print ("Spades")
elif cardSuit == 1:
    print ("Diamonds")
elif cardSuit ==2:
    print ("Hearts")
elif cardSuit == 3:
    print *"Clubs"


if humanCard == 14:
    print ("Ace")
elif humanCard == 13:
    print ("King")
elif humanCard == 12:
    print ("Queen")
elif humanCard == 11:
    print ("Jack")
elif humanCard < 10:
    print (cardValue)

if humanSuit == 0:
    print ("Spades")
elif humanSuit == 1:
    print ("Diamonds")
elif humanSuit == 2:
    print ("Hearts")
elif humanSuit ==3:
    print ("Clubs")


if humanCard > cardValue:
    print("Human Wins!")
    break
elif humanCard < cardValue:
    print('Computer Wins!')
    break
else:
    print ('Its a draw!')
    while True:
        continue

2 个答案:

答案 0 :(得分:1)

我会这样做(因为打印上的f-strings需要python 3.6):

import random

suits        = ['Spades', 'Diamonds', 'Hearts', 'Clubs']
cards        = ['2','3','4','5','6','7','8','9','10','Jack','Queen','King','Ace']
cards_values = [2,3,4,5,6,7,8,9,10,11,12,13,14]

map_cards_values = dict(zip(cards,cards_values))
end_game = False

while not end_game:
    aiCard    = random.choice(cards)
    aiSuit    = random.choice(suits)
    humanCard = random.choice(cards)
    humanSuit = random.choice(suits)

    print(f"AI card is a {aiCard} of {aiSuit}")
    print(f"Human card is a {humanCard} of {humanSuit}")

    if map_cards_values[aiCard] != map_cards_values[humanCard]:
        end_game=True

答案 1 :(得分:0)

  1. 我发现你正在使用while (True)无限循环。使用无限循环是可以的。但是这段代码while True: continue会让你永远陷入困境,因为continue只是说转到下一次迭代。

  2. 您抽取卡片的代码应该在<{strong>内部<{1}}循环中。因为你喜欢在每一轮中抽取新牌。

  3. 以下是您的代码的修改版本。仔细推理逐行。

    while (True)