如何在Python 3.2中检查整数? [英] How to check for an integer in Python 3.2?

查看:98
本文介绍了如何在Python 3.2中检查整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个程序,其中用户输入一个两位数的整数,输出是打印的第二个数字,由第一个数字指示的次数。以下是我到目前为止:

I'm trying to write a program where the user inputs a two-digit integer and the output is the second digit printed the amount of times indicated by the first digit. Here is what I have so far:

number = input('Type two-digit integer \n')
a = int(number)//10
b = int(number)%10
if len(number) != 2:
    print(number, 'is not a two-digit integer')
else:
    print(a*str(b))

我测试了它,只要有人输入数字,它就会按照我的意图行事。如果有人输入,比方说6r,会弹出一条错误信息:

When I test this out it does what I intend it to do as long as someone types in numbers. If someone were to type in, say, 6r, an error message would pop up saying:

a = int(number)// 10

a = int(number)//10

ValueError:基数为10的int()的文字无效:'6r'

ValueError: invalid literal for int() with base 10: '6r'

所以我认为需要放入一些东西代码的第二行来测试输入是否实际上是一个整数,我该怎么做?我会以不同的方式改写它会更好吗?请记住,我正在参加Python的入门课程,这是一个关于我正在练习的中期课程的问题,因此,如果我必须在真正的期中考试这样的话,我就不能使用许多复杂的过程。

So I would assume that something would need to be put in the second line of the code to test if the input is actually an integer, how would I do that? Would I be better off rewriting it in a different way? Please keep in mind that I'm taking an intro course to Python and this is a question on a practice midterm I am taking, so in the case that I would have to answer something like this on the real midterm I can't use a lot of complicated processes.

如果有人输入不是整数的东西,但是由于某种原因,我不知道它给出了非整数到整数的相同消息,并且不能按我的意图运行:

This is something I tried that works if someone types something that isn't an integer, but for some reason that I don't know it gives the same message for non-integers to integers and doesn't function as I intend it to:

number = input('Type two-digit integer \n')
if (isinstance(number, int)) == False:
    print(number, 'is not a two-digit integer')
elif len(number) != 2:
    print(number, 'is not a two-digit integer')
else:
    a = int(number)//10
    b = int(number)%10
    print(a*str(b))

帮助会很大赞赏!

推荐答案

这里最好的选择是o抓住例外。出了问题,这就是设计要处理的异常:

The best option here is to catch the exception. Something has gone wrong, and that's exactly what exceptions are designed to handle:

try:
    ...
except ValueError:
    print("You need to enter an integer!")
    ...

Python有的口头禅请求宽恕而不是权限,所以不要事先检查它是否有效,尝试一下,如果没有,请处理问题工作。

Python has the mantra of it's better to ask for forgiveness than permission, so don't check if it works beforehand, try it, then handle the problem if it doesn't work.

这样可以获得更可靠的代码(检查可能会意外地禁止输入良好的输入,或者通过错误的输入),并使代码读取更好(首先处理正常情况) ,然后处理问题,而不是检查问题,然后处理)。

This makes for more reliable code (the check might accidentally disallow good input, or let through bad input), and makes code read better (you deal with the normal case first, then handle problems, rather than having to check for problems, then process).

这篇关于如何在Python 3.2中检查整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆