我按照冒泡排序重新执行了程序。
def main():
try:
array=[]
file=open(input("Please enter the name of the file you wish to open:" ))
A =file.read().split()
file.close()
n = len(A)
print ("These following", n,"numbers are in the inputted file:\n", A)
for i in range(n):
for j in range(1,n-i):
if A[j-1] < A[j]:
(A[j-1], A[j]) = (A[j],A[j-1])
print("We can now organize it in descending order:\n", A)
except IOError as e:
print("({})".format(e))
Output_File = input("Where would you like to save this data?")
fileObject = open(Output_File, 'a')
fileObject.write(str(Output_File)+'\n')
print("Your file is now saved as", Output_File,". \n Have a nice day!")
fileObject.close()
如果名称== '主要': main()
问题是,它对列表中的每 3 个数字进行排序。所以如果我有 9 个数字,它将有 3 个不同的数字。例如, 1 -3 10 6 5 0 3 -5 20, 将是, ['6', '5', '3', '20', '10', '1', '0', '-5 ','-3']。现在可能出了什么问题?我做对了输出文件吗?