5 examples of 'python integer input' in Python

Every line of 'python integer input' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
5def int_input(input_str=""):
6 while True:
7 try:
8 int(input(input_str))
9 break
10 except ValueError:
11 print("类型出错,请重新输入!")
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

108def integer(prompt=None, empty=False):
109 """Prompt an integer.
110
111 Parameters
112 ----------
113 prompt : str, optional
114 Use an alternative prompt.
115 empty : bool, optional
116 Allow an empty response.
117
118 Returns
119 -------
120 int or None
121 An int if the user entered a valid integer.
122 None if the user pressed only Enter and ``empty`` was True.
123
124 """
125 s = _prompt_input(prompt)
126 if empty and not s:
127 return None
128 else:
129 try:
130 return int(s)
131 except ValueError:
132 return integer(prompt=prompt, empty=empty)
25def input_int(msg):
26 while True:
27 i = input(msg)
28 if is_int(i) and int(i) >= 0:
29 return i
30 elif i == 'q':
31 return None
32 else:
33 print('Invalid input, please try again')
113def readInt(a):
114 while True:
115 try:
116 x = int(raw_input(a))
117 break
118 except ValueError:
119 print "Oops! That was no valid number. Try again..."
120 return int(x)
100def integer(number, *args):
101 """In Python 3 int() is broken.
102 >>> int(bytearray(b'1_0'))
103 Traceback (most recent call last):
104 ...
105 ValueError:
106 """
107 num = int(number, *args)
108 if isinstance(number, str) and '_' in number or isinstance(number, (bytes, bytearray)) and b' ' in number:
109 raise ValueError()
110 return num

Related snippets