Least to greatest


#1

I am trying to write a program that outputs the a list form least to greatest. I wrote it into a file. It keeps giving me the error: ValueError: max() arg is an empty sequence. How can I fix this?

stuff = open("bigsmall.txt", "w")
list = [123,235,53,231,85,795,24,67,8,2]
i = 0
while i <= 11:
x = (max(list))
print(x)
list.remove(x)
stuff.write(str(x))
i += 1
bigsmall.close()
for big in open("bigsmall.txt"):
print(big)

Thanks


#2

Your list has 10 elements.

In every iteration of the while loop, you are removing one element. Your loop will be executed 12 times.

At the beginning of the eleventh iteration, your list is empty.

And that is why you see the error. What's the maximum value in the empty list?


To solve this problem you can change condition of while:

while i <= 9:

or better yet, use len to get the correct number of steps:

length = len(list)
while i < length:

#3

Thank you. I that was what I was missing.


#4

You're very welcome :slight_smile:


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.