快捷导航

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

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]=tempprint(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
    number=int(input("Please input end by #:"))
ValueError: invalid literal for int() with base 10: '#'


Process finished with exit code 1

免责声明:本内容仅代表回答者见解不代表本站观点,请谨慎对待。

版权声明:作者保留权利,不代表本站立场。

回复

使用道具 举报

参与会员1

这里不是因为四位数才出错,是因为在内存while循环中,当你输入了一个非数字的字符串的时候,不能执行int()函数,所以出错,你可以用data.isdigit()判断是否能转化成数字,然后处理退出循环或者继续循环
回复

使用道具 举报

可能感兴趣的问答

发新帖
  • 微信访问
  • 手机APP