Traceback (most recent call last): File "D:\CSC\PYTHON SAMPLE CODES\bubstepbystep.py", line 13, in nlist = nlist + i TypeError: must be str, not int
我无法弄清楚发生了什么里面
。请帮帮我。
import time
nlist = input("Enter series of numbers: ")
swap = len(nlist)
qty = len(nlist)
print("Original:", nlist)
for x in range(qty - 1):
for i in range(swap - 1): #swap
if nlist [i] > nlist [i+1]:
temp = nlist [i]
nlist = nlist + i
nlist [i+1] = temp
print("\nSwapping Index:", i,"and", i+1, "\n\rNew list:", nlist)
time.sleep(3)
else:
print("\nSwapping Index:", i,"and", i+1)
time.sleep(3)
print("Nothing to swap, skipping . . .")
time.sleep(3)
swap -= 1
您在什么时候将从'input(...)'返回的字符串转换为数字列表? 你似乎试图在尝试交换值时增加整个列表'nlist'? python中的交换可以通过以下模式更简单地完成:'a,b = b,a' – Gavin
您不希望'nlist [i] = nlist [i + 1]'在那里吗? –
@JohnnyMopp它也给我一个错误 – Clark