要求用户在Python中输入整数| 限制用户仅输入整数值

input() function can be used for the input, but it reads the value as a string, then we can use the int() function to convert string value to an integer.

input()函数可用于输入,但它将值读取为字符串,然后可以使用int()函数将字符串值转换为整数。

Consider the below program,

考虑下面的程序,

# input a number
num = int(input("Enter an integer number: "))
print("num:", num)

Output

输出量

RUN 1:
Enter an integer number: 10
num: 10

RUN 2:
Enter an integer number: 12.5
Traceback (most recent call last):
  File "main.py", line 2, in <module>
    num = int(input("Enter an integer number: "))
ValueError: invalid literal for int() with base 10: '12.5'

RUN 3:
Enter an integer number: Hello
Traceback (most recent call last):
  File "main.py", line 2, in <module>
    num = int(input("Enter an integer number: "))
ValueError: invalid literal for int() with base 10: 'Hello'

See the output – the program works fine if we input an integer value (RUN 1), but if we input other than integer (RUN 2, RUN3) program returns a ValueError.

看到输出结果–如果输入整数值(RUN 1),则程序运行正常,但是如果输入的不是整数(RUN 2,RUN3),程序将返回ValueError 。

What's next?

下一步是什么?

To handle ValueError, we can use a try-except statement.

为了处理ValueError异常 ,我们可以使用一个尝试 - 除了声明。

See the below program,

参见下面的程序,

# input a number
try:
  num = int(input("Enter an integer number: "))
  print("num:", num)
except ValueError:
    print("Please input integer only...")  

Output

输出量

RUN 1:
Enter an integer number: 10
num: 10

RUN 2:
Enter an integer number: 12.5
Please input integer only...

RUN 3:
Enter an integer number: Hello
Please input integer only...

See the output – the program works fine if we input an integer value (RUN 1), but if we input other than integer (RUN 2, RUN3) program's control transferred to the except block and printed our message. Here, we have handled the exception but still, our task is not completed.

看到输出结果–如果我们输入整数值(RUN 1),则程序运行正常,但是如果输入非整数(RUN 2,RUN3),程序的控制权将转移到except块并打印我们的消息。 在这里,我们已经处理了异常,但是仍然没有完成我们的任务。

What's next?

下一步是什么?

We need to take input until a valid integer value is not entered. For that, we will use while True (for an infinite loop) and will be taking the input till the valid integer.

我们需要接受输入,直到没有输入有效的整数值。 为此,我们将使用while True (用于无限循环),并将输入输入直到有效整数。

See the below program,

参见下面的程序,

限制用户仅输入整数值的程序 (Program for limiting the user to input only integer value)

# input a number
while True:
  try:
    num = int(input("Enter an integer number: "))
    break
  except ValueError:
      print("Please input integer only...")  
      continue

print("num:", num)

Output

输出量

Enter an integer number: 12.5
Please input integer only...
Enter an integer number: Hello world
Please input integer only...
Enter an integer number: Ten
Please input integer only...
Enter an integer number: Twenty Four
Please input integer only...
Enter an integer number: 24
num: 24

Finally, we did it. By using this method we can set the limit to the user to input/accept only integers.

最后,我们做到了。 通过使用此方法,我们可以将限制设置为用户仅输入/接受整数

Recommended posts

推荐的帖子

翻译自: https://www.includehelp.com/python/asking-the-user-for-integer-input-in-python-limit-the-user-to-input-only-integer-value.aspx

  • 13
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值