|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
这是代码
a = input('please').split()
for i in range(len(a)):
max = a[i]
for j in range(len(a)):
if a[i] >= a[j]:
max = a[i]
else:
max = a[j]
#print('max= ',max)
#print(f'weizhi{a.index(max)}')
t = 1
a[a.index(max)] = t
a[a.index(max)] = a[0]
a[0] = t
print(a)
这是报错
please1 2 3 4 5 6 7 8 9 10
Traceback (most recent call last):
File "C:/Users/hp/AppData/Roaming/JetBrains/PyCharmCE2022.1/scratches/scratch.py", line 28, in <module>
a[a.index(max)] = a[0]
ValueError: '10' is not in list
因为你输入了 1 2 3 4 5 6 7 8 9 10 ,max 为 10,当 a[a.index(max)] = t 时
将 a.index(max) 位置上的元素给覆盖赋值了,导致下一次寻找 a.index(max) 时找不到列表中 为 max 的元素而报错
你代码的目的是想达到什么效果?
|
|