列表不是用Python打印的
我的以下代码旨在收集用户的数字输入,将其放入列表中,并对其进行排序:列表不是用Python打印的,python,Python,我的以下代码旨在收集用户的数字输入,将其放入列表中,并对其进行排序: def build_list(): """Builds a list by continually collecting input from the user until 'done' is provided.""" raw_list = [] while True: item = input("Enter a numeric value: ") if item
def build_list():
"""Builds a list by continually collecting input from the user until 'done' is provided."""
raw_list = []
while True:
item = input("Enter a numeric value: ")
if item.lower() == "done":
break
try:
item = int(item)
except ValueError:
print("That wasn't a valid number. Try again or enter 'done' to stop making the list.")
else:
raw_list.append(item)
return raw_list
def sort_list(unsorted_list):
sorted_list = unsorted_list.sort()
return sorted_list
def main():
my_list = build_list()
my_list = sort_list(my_list)
print(my_list)
if __name__ == '__main__':
main()
这段代码正在打印“无”,而不是打印我的排序列表。我在这里遗漏了哪一步?排序()功能(以及反向()功能)在“适当位置”工作,并始终返回无
你应该写:
def排序列表(未排序列表):
已排序列表=列表(未排序列表)#本地副本(如果需要)
已排序的\u list.sort()
返回已排序的列表
您还可以使用sorted()
函数:
def排序列表(未排序列表):
已排序的\u列表=已排序(未排序的\u列表)
返回已排序的列表
请参阅:排序
对就地列表排序并返回无
。调用sort
而不返回:
def main():
我的列表=构建列表()
我的列表。排序()
打印(我的列表)
或使用排序后的
:
def main():
我的列表=构建列表()
my_list=已排序(my_list)#此处
打印(我的列表)
.sort()
返回none这看起来像的副本。