我正在制作一个名为“dumbstuff”的模块,只是出于一种爱好,似乎无法确定问题所在。该函数设置为接受一个列表和一个运算符,以按升序或降序对列表进行排序。当我运行该功能时,出现一个空白屏幕,我已经用了一段时间,但无法弄清楚出了什么问题。
这是“dumbstuff”模块中的排序函数:
def sortlist(rawlist: list, operator: str, ifprint: bool):
looped = 0
done = 0
index = 0
sortedList = []
while (done == 0):
index = 0
looped = 0
#ascending sort
if (operator == "<"):
while (index < len(rawlist) - 1):
if (rawlist[index] > rawlist[index + 1]):
temp = rawlist[index]
rawlist[index] = rawlist[index + 1]
rawlist[index + 1] = temp
looped += 1
if (looped == 0): done = 1
sortedList = rawlist
#descending sort
if (operator == ">"):
while (index < len(rawlist) - 1):
if (rawlist[index] < rawlist[index + 1]):
temp = rawlist[index + 1]
rawlist[index + 1] = rawlist[index]
rawlist[index] = temp
looped += 1
if (looped == 0): done += 1
sortedList = rawlist
if (ifprint == True):
print(sortedList)
这是我试图运行它的代码,它创建了一个包含 20 个随机整数的数组,
import random
import dumbstuff as ds
array = []
index = 0
while (index < 20):
array.append(random.randint(0, 20))
index += 1
ds.sortlist(array, "<", ifprint=True)
input()
但是,代码似乎永远不会返回,也永远不会向屏幕输出任何内容。
紫衣仙女
相关分类