簡體   English   中英

我該如何解決這個問題,x 不在列表中

[英]how do i fix this, x not in list

這是這個子程序的代碼

def elimination():
    global name
    global contestants
    global tempContestants
    global lipsync_songs
    global bottom_two
    bottom = tempContestants[0:len(tempContestants)-1]
    tempContestants.remove(bottom)
    bottom1 = tempContestants[0:len(tempContestants)-1]
    tempContestants.remove(bottom1)
    bottom_two = [bottom, bottom1]
    print(bottom_two[0], "and", bottom_two[1], "I'm sorry my dears but you are up for elimination")
    lipsync()
    eliminated = bottom_two[random.randint(0,len(bottom_two)-1)]
    bottom_two.remove(eliminated)
    safe = str(bottom_two)
    print("Ladies, I have made my decision")
    print(safe+", shantay you stay!")
    print(eliminated+", sashay away!")
    contestants.remove(eliminated)
    if eliminated == name:
        print("You have been eliminated!")
        quit()

這是錯誤信息

    tempContestants.remove(bottom)
ValueError: list.remove(x): x not in list

這是什么意思,我該如何解決?

這會重現您的錯誤:

In [644]: [1,2,3].remove(4)                                                                    
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-644-181ce8c0fac7> in <module>
----> 1 [1,2,3].remove(4)

ValueError: list.remove(x): x not in list
In [645]: [1,2,3].remove(3) 

看起來bottom是數組元素的列表;

In [689]: [1,2,3].remove([1,2])                                                                
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-689-9c95562dca44> in <module>
----> 1 [1,2,3].remove([1,2])

ValueError: list.remove(x): x not in list

===

bottom = tempContestants[0:len(tempContestants)-1]
tempContestants.remove(bottom)

In [691]: x = [1,2,3,4]                                                                        
In [692]: bottom = x[0:len(x)-1]                                                               
In [693]: bottom                                                                               
Out[693]: [1, 2, 3]

您可以迭代刪除項目:

In [694]: for i in bottom: x.remove(i)                                                         
In [695]: x                                                                                    
Out[695]: [4]

或者只選擇要保留的切片:

In [696]: x = [1,2,3,4] 
In [698]: x[-1:]                                                                               
Out[698]: [4]

此代碼將隨機丟棄最后兩名參賽者之一。 我不知道這是不是你的意圖,但我會像稻草人一樣把它扔掉。

import random

def elimination():
    # get last two contestants
    candidates = tempContestants[-2:]
    if len(candidates) != 2:
        print("Sorry, not enough players")
        quit()
    print("{} and {}, I'm sorry my dears but you are up for elimination".format(
            *candidates))
    lipsync()

    # select and and remove the loser
    eliminated = random.choice(candidates)
    candidates.remove(eliminated)
    safe = candidates[0]
    tempContestants.remove(eliminated)

    # report the news
    print("""Ladies, I have made my decision.
{}, shantay you stay!
{}, sashay away!""".format(safe, eliminated))

    # decide fate of game
    if eliminated == name:
        print("You have been eliminated!")
        quit()

def lipsync(): pass
tempContestants = ["A", "B", "C", "D", "E", "F"]
name = "F"
elimination()

運行此程序,由於隨機選擇,您將獲得兩個可能的輸出。 一次運行結果

E and F, I'm sorry my dears but you are up for elimination
Ladies, I have made my decision.
E, shantay you stay!
F, sashay away!
You have been eliminated!

暫無
暫無

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

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