簡體   English   中英

為什么list.remove(value)根本不起作用?

[英]Why is list.remove(value) not working at all?

我有以下代碼:

def game():
    for i in range(0, 10):
        questions = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
        randomInt = choice(questions)
        print(formatIt(songs[randomInt], artists[randomInt]))
        questions.remove(randomInt)

formatIt是我自己的功能之一, choicerandom.choice

一切正常,直到在底部的questions.remove為止,它什么也不做(沒有錯誤消息)。 我嘗試將列表項更改為字符串; 使用集合而不是列表,但這與random.choice不兼容,並將變量更改為實際值,但列表保持不變。

首先有兩件事:

questions.remove(questions[randint(0,9)])   # otherwise it can try removing like 65 or something

其次:我不知道它是否是預期的,但是在循環的每一步中,您都將問題帶回到其原始狀態。 如果您不希望這樣做,請將此行放在以下位置:

def game():
    questions = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    for i in range(0, 10):
        # rest of code

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM