I want to accept input from a user and store them in the empty list and sort that values and then print it but I'm getting last value printed.
sort_value = [].sort(key=int)
chances = 1
while chances <= 5:
a = int(input("Enter a number"))
if a >= 0:
sort_value = a
chances += 1
print(sort_value)
sort_value = [].sort()
will assign None
to sort_value
since sort()
doesn't have return
statement.
sort_value = a
will assign a to sort_value
making it an int
instead of adding it to a list.
You need to sort the list after you insert items to it, and you need to append to the list instead of assigning it
sort_value = []
chances = 1
while chances <= 5:
a = int(input("Enter a number"))
if a >= 0:
sort_value.append(a)
chances += 1
sort_value.sort()
print(sort_value)
sort_value = []
chances = 1
while chances <= 5:
a = int(input("Enter a number"))
if a >= 0:
sort_value.append(a)
chances += 1
sort_value.sort()
print(sort_value)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.