怎样判断用户输入的是否为数字 续
2011-01-20 21:06
591 查看
判断输入是否为数字 续
这里用到了检测与处理异常的try-except语句。
下面是一个例子,在Python交互环境执行的:
>>> a = int(raw_input('Please input an integer ... '))
Please input an int number ... t
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 't'
那么,如果使用了try-except语句,结果就是这样的。
>>> try:
... a = int(raw_input('Please input an integer ... '))
... except ValueError, error_info:
... print error_info
...
Please input an int number ... t
invalid literal for int() with base 10: 't'
我们可以把这段代码更加完善成,如果用户没有输入数字,就一直叫用户重新尝试。
>>> trigger = True
>>> while trigger:
... try:
... trigger = False
... a = int(raw_input('Please input an integer ... '))
... except ValueError:
... trigger = True
... print 'You did not input an integer, try again ... '
...
Please input an integer ... a
You did not input an integer, try again ...
Please input an integer ... 1.1
You did not input an integer, try again ...
Please input an integer ... *
You did not input an integer, try again ...
Please input an integer ... @
You did not input an integer, try again ...
Please input an integer ... 0.
You did not input an integer, try again ...
Please input an integer ... 0
>>>
这里有个新问题,程序会把数字0.也认为不是一个整数。会有更好的处理办法吗?
下面是检测与处理异常的try-except语句的一个典型例子。能帮助你理解try-except语句。
>>> 1/0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: integer division or modulo by zero
>>> try:
... 1/0
... except ZeroDivisionError, e:
... print e
...
integer division or modulo by zero
>>>
这里用到了检测与处理异常的try-except语句。
下面是一个例子,在Python交互环境执行的:
>>> a = int(raw_input('Please input an integer ... '))
Please input an int number ... t
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 't'
那么,如果使用了try-except语句,结果就是这样的。
>>> try:
... a = int(raw_input('Please input an integer ... '))
... except ValueError, error_info:
... print error_info
...
Please input an int number ... t
invalid literal for int() with base 10: 't'
我们可以把这段代码更加完善成,如果用户没有输入数字,就一直叫用户重新尝试。
>>> trigger = True
>>> while trigger:
... try:
... trigger = False
... a = int(raw_input('Please input an integer ... '))
... except ValueError:
... trigger = True
... print 'You did not input an integer, try again ... '
...
Please input an integer ... a
You did not input an integer, try again ...
Please input an integer ... 1.1
You did not input an integer, try again ...
Please input an integer ... *
You did not input an integer, try again ...
Please input an integer ... @
You did not input an integer, try again ...
Please input an integer ... 0.
You did not input an integer, try again ...
Please input an integer ... 0
>>>
这里有个新问题,程序会把数字0.也认为不是一个整数。会有更好的处理办法吗?
下面是检测与处理异常的try-except语句的一个典型例子。能帮助你理解try-except语句。
>>> 1/0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: integer division or modulo by zero
>>> try:
... 1/0
... except ZeroDivisionError, e:
... print e
...
integer division or modulo by zero
>>>
相关文章推荐
- 怎样判断用户输入的是否为数字
- 判断用户输入的数字是否为99,并将不是数字之和打印出来,巧妙使用break和continue。
- js判断用户输入的内容是否为纯数字
- 编写一个Java应用程序。用户从键盘输入一个1—9999之间的数,程序将判断这个数是几位数,并判断这个数是否是回文数。回文数是指将该数含有的数字逆序排列后得到的数和原数相同,例如12121、3223都
- js中利用isNaN函数判断用户输入是否为数字
- shell 如何判断用户从键盘输入的变量是否为数字
- c#数字验证码功能,以及判断用户输入是否正确。
- 编写一个Java应用程序。用户从键盘输入一个1—9999之间的数,程序将判断这个数是几位数, 并判断这个数是否是回文数。回文数是指将该数含有的数字逆序排列后得到的数和原数相同, 例如12121、3
- 判断用户输入是否为数字
- 2. 编写一个Java应用程序,用户从键盘输入一个1~9999之间的数,程序将判断这个数是几位数,并判断这个数是否是回文数。回文数是指将该数含有的数字逆序排列后得到的数和原数相同,例如12121、32
- 【摘录】关于判断用户输入是否数字字母等思路及源码
- 判断用户输入是否为数字
- C++ 如何一次性获取用户输入并判断是否为数字?
- [Tool]判断用户输入是否符合规范(大小写0~9数字)
- 判断用户输入是否是数字
- 43.编写一个程序,判断用户输入的字符是否是数字,若是数字,则输出“a numerical character”
- 判断用户输入是否为数字?
- 第二章作业51.4(编写一个程序,判断用户输入的字符是否为数字,若是数字,则输出“a numerical charater”,否则输出“other charater”。)
- C++判断用户输入是否为数字?
- 编写程序,判断用户输入的数字是否完全数.所谓“完全数”是指整数n的所有因子(不包括n)之和等于n自身。例如28的因子为1、2、4、7、14,而28=1+2+4+7+14,因此28是“完全数”。