list=[]
number=input("Please?input?end?by?#:")
while?number!='#':
????list.append(number)
????number=int(input("Please?input?end?by?#:"))
length=len(list)
for?i?in?range(0,length-1):
????for?j?in?range(0,length-i-1):
????????if?list[j]>list[j+1]:
????????????temp=list[j]
????????????list[j]=list[j+1]
????????????list[j+1]=temp
print(list)測試:Please input end by #:56Please input end by #:8888Please input end by #:#Traceback (most recent call last):? File "D:/Python/workspace/Pybasis/Buble_sort.py", line 5, in <module>? ? number=int(input("Please input end by #:"))ValueError: invalid literal for int() with base 10: '#'Process finished with exit code 1
1 回答
已采納

MAYA_MUYI
TA貢獻13條經驗 獲得超5個贊
這里不是因為四位數才出錯,是因為在內存while循環中,當你輸入了一個非數字的字符串的時候,不能執行int()函數,所以出錯,你可以用data.isdigit()判斷是否能轉化成數字,然后處理退出循環或者繼續循環
添加回答
舉報
0/150
提交
取消