【发布时间】:2020-02-21 04:30:19
【问题描述】:
我正在尝试使用 python 使用用户输入创建一个列表,然后我想使用气泡函数对列表进行排序,但是当我运行代码时,我得到一个错误,说函数没有属性 homework3。有人可以帮助运行代码并对列表进行排序吗?
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”的写法应该和调用之前所有的函数如“input”或“print”类似。
-
不要在内置 python 类型和函数之后命名您的代码对象(如名为
bubble的函数的参数list)。
标签: python python-3.7 bubble-sort