Python 让气泡函数运行
我试图使用python使用用户输入创建一个列表,然后我想使用气泡函数对列表进行排序,但是当我运行代码时,我得到一个错误,说函数没有属性homework3。有人可以帮助运行代码并对列表进行排序吗Python 让气泡函数运行,python,python-3.7,bubble-sort,Python,Python 3.7,Bubble Sort,我试图使用python使用用户输入创建一个列表,然后我想使用气泡函数对列表进行排序,但是当我运行代码时,我得到一个错误,说函数没有属性homework3。有人可以帮助运行代码并对列表进行排序吗 def bubble(list): index_length=len(list)-1 sorted= False while not sorted: sorted= True for i in range (0,index_length):
def bubble(list):
index_length=len(list)-1
sorted= False
while not sorted:
sorted= True
for i in range (0,index_length):
if list[i]>list[i+1]:
sorted=False
list[i],list[i+1]=list[i+1], list[i]
return list
homework3=[]
number_value=int(input('How many numbers do you want to sort?:'))
for j in range(number_value):
value=int(input('Please enter your number value then press enter:'))
homework3.append(value)
print()
print(bubble.homework3)
我相信这个代码对冒泡排序很有用。但不确定为什么要打印(bubble.homework3)。改用
print(bubble(homework3))
您需要像这样调用函数:- 泡泡(家庭作业3)
将语法更改为此,它将起作用调用“bubble”的方式应与调用以前所有函数(如“input”或“print”)的方式类似。不要在内置python类型和函数之后命名代码对象(如名为
bubble
的函数的参数列表
)。