简体   繁体   中英

how do i fix this, x not in list

this is the code for this sub routine

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()

and here is the error message

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

what does this mean and how do i fix it?

This reproduces your error:

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) 

Looks like bottom is a list of elements of array;

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]

You could remove the items iteratively:

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

or just select the slice that you want to keep:

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

This code will randomly discard one of the last two contestants. I don't know if this is your intent, but I will throw it out as a straw man.

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()

Running this, you will get two possible outputs because of the random choice. One run results in

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM