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
提交
取消