繁体   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