这个数据应该怎么输入才对 为啥我输入以后就出来红色的东西(好吧我承认我上课没听课)
程序里5个数,是依次输入的。
代码中是用for
循环调用了5次input()
,因此你需要输入5次,每次输入回车换行,便是完成依次输入。
#!/usr/bin/nve python
# coding: utf-8
nums = list(map(int, input('\n输入数组(如[8 3 2):').split()))
print('\n输入数组:', nums)
print(f"\n插入排序过程:\n{'':~^50}")
for i in range(1, len(nums)):
j = i - 1
if nums[i] > nums[j]: # 算法优化:为有序,不用执行后面的比对插入代码。直接跳过,执行下一循环。
continue
print(f"\n索引:{i},比对插入排序数字:{nums[i]}\n列表状态:有序{nums[:i]},无序{nums[i+1:]}")
temp = nums.pop(i) # 用pop()方法取出比对排序的数字。
while j >= 0:
if temp > nums[j]: # 在比比对数小的数后插入。
nums.insert(j+1, temp)
break # 插入后退出循环。
j -= 1
if j < 0: # 比所有有序数小,插入数列最前端。
nums.insert(0, temp)
print(f"\n{'':~^50}\n插入排序后数组:{nums}\n")
input('提示输入:')
>>>
>>> help(input)
Help on built-in function input in module builtins:
input(prompt=None, /)
Read a string from standard input. The trailing newline is stripped.
The prompt string, if given, is printed to standard output without a
trailing newline before reading input.
If the user hits EOF (*nix: Ctrl-D, Windows: Ctrl-Z+Return), raise EOFError.
On *nix systems, readline is used if available.
(END)
你必须每次输入一个数据按下回车。你要输入5个数据,就需要输入5次,按下5次回车。
你传入的参数存在问题,不要一行全部传入,应该每次你输入一个数字后回车一下,再输入下一个数字