为什么输入四位数时,会出错

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 #:56

Please input end by #:8888

Please 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


qq_莫非
浏览 652回答 1
1回答

MAYA_MUYI

这里不是因为四位数才出错,是因为在内存while循环中,当你输入了一个非数字的字符串的时候,不能执行int()函数,所以出错,你可以用data.isdigit()判断是否能转化成数字,然后处理退出循环或者继续循环
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python