del或listl.remove不工作python 3.5

时间:2016-11-29 15:41:37

标签: python-3.5 del

这个程序必须订购一个数字列表但每次我成为一个错误你能帮我解决吗?我希望有人有个主意。我hzave也试图向我们列出[(元素或数字的数量)]

# list = list with the none ordert number
# newlist = with the ordert numbers
# pnumbver = privious number
# add = new number for list
# numberelemente = how many numbers get in list
# length = length of list
# i = counting up for the stop




from random import randint

list = []
newlist = []
numberelemente = 10
while numberelemente > 0:
    add = randint(-100, 100)
    list.append(add)
    numberelemente = numberelemente - 1

print(list)

pnumber=list[0]
length = len(list)
i = 0


while i < length:
    for zahl in list:
       if number < pnumber:
            pnumber = number
    list.remove(pnumber)
    newlist.append(pnumber)
    i = i+1

print(newlist)

但我变成了这个错误,我每次都成为他们

eenter code her>>> runfile('C:/Users/Max/Desktop/python/liste ordnen.py', wdir='C:/Users/Max/Desktop/python')
[89, 46, 68, -30, 93, 38, -73, 91, 33, -69]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\Max\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile
    execfile(filename, namespace)
  File "C:\Users\Max\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)
 File "C:/Users/Max/Desktop/python/liste ordnen.py", line 29, in <module>
    list.remove(vzahl)
ValueError: list.remove(x): x not in list

1 个答案:

答案 0 :(得分:0)

我从不说你把pnumber添加到列表中所以它不能删除它。不打算用。

list.append(pnumber)

这是list.remove()做的事情

  

list.remove(x)的

     

从列表中删除值为x的第一个项目。如果没有这样的项目,则会出错。

- https://docs.python.org/3/tutorial/datastructures.html

希望这有帮助。