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
推薦的帖子
Read input as an integer in Python
在Python中將輸入讀取為整數
Read input as a float in Python
在Python中以浮點形式讀取輸入
Parse a string to float in Python (float() function)
解析要在Python中浮動的字符串(float()函數)
How do you read from stdin in Python?
您如何從Python的stdin中讀取信息?
Asking the user for input until a valid response in Python
要求用戶輸入直到Python中的有效響應
Input a number in hexadecimal format in Python
在Python中以十六進制格式輸入數字
Input a number in octal format in Python
在Python中以八進制格式輸入數字
Input a number in binary format in Python
在Python中以二進制格式輸入數字
How to get the hexadecimal value of a float number in python?
如何在python中獲取浮點數的十六進制值?
Convert an integer value to the string using str() function in Python
使用Python中的str()函數將整數值轉換為字符串
Convert a float value to the string using str() function in Python
使用Python中的str()函數將浮點值轉換為字符串
Input and Output Operations with Examples in Python
使用Python中的示例進行輸入和輸出操作
Taking multiple inputs from the user using split() method in Python
使用Python中的split()方法從用戶獲取多個輸入
Fast input / output for competitive programming in Python
快速輸入/輸出,可在Python中進行有競爭力的編程
Precision handling in Python
Python中的精確處理
Python print() function with end parameter
帶有結束參數的Python print()函數
翻譯自: https://www.includehelp.com/python/asking-the-user-for-integer-input-in-python-limit-the-user-to-input-only-integer-value.aspx