Python 3.x 如何在python中从小到大打印数字?
我有一个关于输入一些数字并从小到大排列的家庭作业 这里的代码显示了错误,我不确定这意味着什么以及如何修复它Python 3.x 如何在python中从小到大打印数字?,python-3.x,numbers,Python 3.x,Numbers,我有一个关于输入一些数字并从小到大排列的家庭作业 这里的代码显示了错误,我不确定这意味着什么以及如何修复它 # -*- coding: utf-8 -*- """ Spyder Editor This is a temporary script file. """ list1 = [] def disp_data(): for items in datas: print(items,end=" ") print() while True: h=inpu
# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
list1 = []
def disp_data():
for items in datas:
print(items,end=" ")
print()
while True:
h=input("input a number(press enter to exit):")
if h == "":
break
else:
datas = list1.append(h)
print('before:',end =" ")
disp_data()
n = len(datas)-1
for i in range(0,n):
for j in range(0,n-i):
if (datas[j]>datas[j+1]) :
datas[j],datas[j+1]=datas[j+1],datas[j]
print("after:",end=" ")
disp_data()
TypeError: 'NoneType' object is not iterable
我已经写了一篇:
def disp_data():
for items in datas:
print(items,end=" ")
print()
datas = [3,5,2,1]
print('before',end =" ")
disp_data()
n = len(datas)-1
for i in range(0,n):
for j in range(0,n-i):
if (datas[j]>datas[j+1]) :
datas[j],datas[j+1]=datas[j+1],datas[j]
print("after",end=" ")
disp_data()
任何想法或错误语法,请编辑或回答。谢谢,列表方法E append不会返回None,因此datas=list1.append使数据为None。一个简单的修复方法是重命名list1数据:
[……]
datas = []
while True:
h = input("input a number(press enter to exit):")
if h == "":
break
else:
datas.append(h)