def selectionSort(lst):
with lst as f:
nums = [int(line) for line in f]
for i in range(len(nums) - 1, 0, -1):
maxPos = 0
for position in range(1, i + 1):
if nums[position] > nums[maxPos]:
maxPos = position
value = nums[i]
nums[i] = nums[maxPos]
nums[maxPos] = value
def main():
textFileName = input("Enter the Filename: ")
lst = open(textFileName)
selectionSort(lst)
print(lst)
main()
好的,感谢 hcwhsa 帮助我完成阅读文件并将它们全部放在一行中。
当我运行该代码时,我收到以下错误:
<_io.TextIOWrapper name='numbers.txt' mode='r' encoding='UTF-8'>
文本文件:
67
7
2
34
42
有什么帮助吗?谢谢。