所以我在一些作业上有这个问题。
3)编写一个程序,其中
50
-55
的随机数(0
至100
)sort()
,sorted()
,sum()
,必须编写自己的代码)这是我当前的代码:
import random
myFile = open("Median.txt", "w")
for i in range(random.randint(50,55)):
myFile.write(str(random.randint(0,100))+'\n')
myFile.close()
myFile = open("Median.txt", "r")
nums = []
with open("Median.txt") as myFile:
nums = myFile.read().splitlines()
def sort(nums):
for num in range(len(nums)-1,0,-1):
for i in range(num):
if nums[i] > nums[i+1]:
temp = nums[i]
nums[i] = nums[i+1]
nums[i+1] = temp
sort(nums)
print(nums)
我的问题是我的输出看起来像这样
['1', '1', '100', '13', '14', '15', '17', '18', '18', '20', '20', '22', '22', '27', '35', '39', '40', '42', '44', '45', '5', '51', '54', '57', '57', '58', '59', '61', '62', '64', '64', '67', '68', '69', '69', '7', '70', '71', '72', '72', '73', '76', '79', '79', '84', '87', '9', '92', '95', '95', '97', '99']
数字100
和7
乱序。
我该如何解决?