[英]How to accept, store and sort the values in while loop?
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 = [].sort()
将None
分配给sort_value
因为sort()
没有return
语句。
sort_value = a
will assign a to sort_value
making it an int
instead of adding it to a list. sort_value = a
将 a 分配给sort_value
使其成为int
而不是将其添加到列表中。
You need to sort the list after you insert items to it, and you need to append to the list instead of assigning it您需要在插入项目后对列表进行排序,并且您需要将 append 分配给列表而不是分配它
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.