Python 如何修复此问题,x不在列表中
这是此子程序的代码Python 如何修复此问题,x不在列表中,python,typeerror,Python,Typeerror,这是此子程序的代码 def elimination(): global name global contestants global tempContestants global lipsync_songs global bottom_two bottom = tempContestants[0:len(tempContestants)-1] tempContestants.remove(bottom) bottom1 = temp
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)
===
您可以迭代删除这些项:
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!
什么是选手?你想让那一行做什么?@MichaelBianconi或只是
临时参赛者[:-1]
-假设OP想要一个新的名单,除了最后一个参赛者。你想倒数第二个
成为最后一个参赛者吗?然后就是bottom=tempCompetitors[-1]
。顺便说一句,您不需要所有这些global
语句<代码>全局仅在分配值(等号左侧的变量)时有用。当您所做的只是引用一个变量时,python会在第一次编译函数时判断它是本地的还是全局的。@t我不确定我到底是不是最后一个参赛者。这没有帮助。我试图向您展示tempCompetities.remove(bottom)
会产生此错误,因为bottom
(它的价值是什么)不在tempCompetities
中。您当然可以找出原因!或者您可以将此remove
包装在try/except
块中。好的,看起来您想删除除最后一个之外的所有内容-然后只需适当地对其进行切片。请参阅我的编辑。它说不能将序列乘以类型为内置函数或方法的无int的整数??如何操作?t汉克,我明天试试,告诉你是否可以works@RyanLaker-我编辑以显示我的完整测试代码,它可以正常工作,没有错误。问题在于您没有显示的代码。
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!