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
언제 '입력 (...)'에서 반환 된 문자열을 숫자 목록으로 변환합니까? 값을 바꿀 때 nlist 전체 목록을 증가 시키려고하는 것 같습니다. 'a, b = b, a' – Gavin
'nlist [i] = nlist [i + 1]'을 원하지 않으십니까? –
@JohnnyMopp 또한 오류가 발생합니다 – Clark