TypeError: must be str, not int in bubble sort
TypeError: must be str, not int in bubble sort
我的 Python 排序算法(冒泡排序)代码中出现此类错误
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
你的问题是你的输入,我认为:将它映射到适当的整数,事情看起来好多了。另外,您的交换代码不正确:
import time
nlist = list(map(int, input("Enter series of numbers: ").split()))
nlist
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]
print(temp)
nlist [i] = nlist [i+1]
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
print("Final:", nlist)
我的 Python 排序算法(冒泡排序)代码中出现此类错误
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
你的问题是你的输入,我认为:将它映射到适当的整数,事情看起来好多了。另外,您的交换代码不正确:
import time
nlist = list(map(int, input("Enter series of numbers: ").split()))
nlist
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]
print(temp)
nlist [i] = nlist [i+1]
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
print("Final:", nlist)