为什么我的程序会重演

时间:2017-04-13 16:46:15

标签: python

我试图创建一个游戏,你可以在其中发现某些东西,但是当它生成新的东西时,它总是重复一个,我不知道为什么。所以任何解决方案都将非常感激。谢谢

import random
cards=["3","helicopter","skip","chimmney","balloon","plane","bridge"]
while list is not []:
    card1=random.choice(cards)
    cards.remove(card1)
    card2=random.choice(cards)
    cards.remove (card2)
    print "card 1 = ",card1
    print "card 2 = ",card2
    choice=raw_input("type the card that you have found: ")
    if choice ==card1:
        card1=random.choice(cards)
        print "card 1 = ",card1
    else:
       card2=random.choice(cards)
       print "card 2 = ",card2

1 个答案:

答案 0 :(得分:-2)

您的程序会导致无限循环,因为它一直有效,直到数组为空,但您永远不会从中删除所有对象。

import random
cards=["3","helicopter","skip","chimmney","balloon","plane","bridge"]
running = True
while running:
    card1=random.choice(cards)
    cards.remove(card1)
    card2=random.choice(cards)
    cards.remove (card2)
    print "card 1 = ",card1
    print "card 2 = ",card2
    choice=raw_input("type the card that you have found: ")
    if choice ==card1:
        card1=random.choice(cards)
        print "card 1 = ",card1
        running = False
    else:
        card2=random.choice(cards)
        print "card 2 = ",card2
        running = False