Python 使用所选的2和名称更改元素

Python 使用所选的2和名称更改元素,python,Python,如何根据列表中的元素所选的编号更新其中一个元素,并更新到 他们选择的名字。例如,如果他们选择了2,和钢铁侠,我如何改变元素2(杀手女孩) 为了他们选择的名字(钢铁侠) 谢谢你的帮助 #Question 1 heroes = ["Batman","Wonder Woman","Superman","Spiderman"] #Question 2 print("Current pilot: ",her

如何根据列表中的元素所选的编号更新其中一个元素,并更新到
他们选择的名字。例如,如果他们选择了2,和钢铁侠,我如何改变元素2(杀手女孩) 为了他们选择的名字(钢铁侠)

谢谢你的帮助

#Question 1
heroes = ["Batman","Wonder Woman","Superman","Spiderman"]
#Question 2
print("Current pilot: ",heroes[0])
#Question 3
print("Co pilot: ",heroes[1])
#Question 4
heroes[2] = "Hit girl"
print(heroes)
['Batman', 'Wonder Woman', 'Hit girl', 'Spiderman']
#Question 5
heroes.append("Cat Woman")
heroes.append("The Flash")
print(heroes)
['Batman', 'Wonder Woman', 'Hit girl', 'Spiderman', 'Cat Woman', 'The Flash']

#Extension
number = input ("What number do you want to replace from 0-5?: ")
If number == 0:
name = input ("With what name do you want to replace number " + number +"?: ")
你问了一个
号码
是对的。然后,您要求一个
名称
将列表英雄索引中的值替换为该
名称
。然后您只需通过执行
heros[int(number)]=name来替换


请注意,您需要
int()
,因为您输入的数字是一个字符串,它必须是一个整数才能用作索引号。

除了变量之外,您的操作与以前相同。你试过什么了吗?好像你想让别人帮你做作业。你试过什么吗?sourcerer,是的,我当然试过了,我也试过在网上搜索,但没有找到我要找的东西,这就是为什么我在这里问的原因。也许下次我就不用问了,或者如果我不得不再问的话,请不要回复我。再次感谢你的批评,谢谢你的帮助。谢谢,没问题。如果有帮助的话,请考虑接受答案(点击我的答案的灰色记号左上方,它会变成绿色!)如果答案解决了你的问题,考虑一下。
heroes = ["Batman","Wonder Woman","Superman","Spiderman"]

print("Current pilot: ",heroes[0])
print("Co pilot: ",heroes[1])

heroes[2] = "Hit girl"

heroes.append("Cat Woman")
heroes.append("The Flash")

print (heroes)

number = input ("What number do you want to replace from 0-5?: ")
name = input ("With what name do you want to replace number " + number +"?: ")

heroes[int(number)] = name

print (heroes)