2017-04-13 104 views
-2

我試圖創建一個遊戲,在這個遊戲中你可以找到某些東西,但是當它產生新的項目時,它總是重複一個,我不知道爲什麼。所以任何解決方案將不勝感激。謝謝爲什麼我的程序會重複

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 
+0

什麼是'list'你的意思是'卡'嗎? – MooingRawr

+0

** list **是一個預定義的類型。你想做什麼? – Prune

+0

當列表不是[]時,將'列表'更改爲'卡片'。請注意,前者正在檢查內置'list'是否爲'[]',這從來就不是這種情況。 – khachik

回答

-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 
+0

'列表不是[]'總是評估'真',因此循環不會退出。 –

+2

爲什麼你甚至在這裏使用'while'? – gurka

+0

非常感謝你,我現在明白了 –

相關問題