Errors


# sorting a list

# empty list

numbers = []

while additions != 0:
    additions = input("enter the number,0 to stop: ")

sort_list = numbers.sort()

print (sort_list)
   

I am getting two errors

  1. in the while statement, the additions is showing name is not defined, which my IDE says This inspection warns about local variables referenced before assignment
    2.And in the sort list variable it is showing an error that highlights situations when an assignment is done on a function call but the inferred function doesn’t return anything.

the first time the loop condition is encountered:

while additions != 0:

the variable hasn’t been defined yet, that happens inside the loop

.sort() sorts the list in place/memory, so the list is modified but the method itself does not return anything

so how do l define additions, which is an input outside the loop, l want the input to keep happening until someone writes 0

so do you mean the .sort() method, you use it directly in a print statement?

python unfortunately doesn’t have a do .. while loop, so you could also simply input one time before your loop

nope, because print will print the returned value

you will need to sort first, and then you can print

even after declaring additions the loop keeps running , even if l enter zero

Thanks

what data type does input() give back?

Check your notes :grinning:


# sorting a list

# empty list

numbers = []

additions = input("enter the number,0 to stop: ")
numbers.append(additions)
while additions != "0":
    additions = input("enter the number,0 to stop: ")
    numbers.append(additions)
    print (numbers)

prep_list = numbers.sort()

print(prep_list)

this is not working now

please check documentation:

https://docs.python.org/3/howto/sorting.html

1 Like

my mistake was to store in a variable, got the solution, thanks