我在氣泡排序後重新執行了程序。將文件中的元素按降序排列而沒有內置函數
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()
如果名 == '主要': 主要每3個列表中的號碼()
的問題是,它排序。所以如果我有9個號碼,它會有3個不同的號碼。例如,1 -3 10 6 5 0 3 -5 20將是['6','5','3','20','10','1','0','-5 ','-3']。現在有什麼可能是錯誤的?我做了輸出文件嗎?
那麼,你正在格式化你的代碼示例錯誤的一個。 – millimoose