I have 3 different functions I have tried in my programs. I want a function which continues to loop until an integer is entered. Which would be the best to use? Or is there a better one?
def valid_int(question):
"""Creates a loop until an integer is entered"""
py_version = find_version()
if py_version == 3:
response = input(question)
while not response.isdigit():
print("\nInvalid input! Input must be an integer")
response = input(question)
return int(response)
else:
response = raw_input(question)
while not response.isdigit():
print("\nInvalid input! Input must be an integer")
response = raw_input(question)
return int(response)
def valid_int(question):
"""Creates a loop until an integer is entered"""
py_version = find_version()
if py_version == 3:
while True:
response = input(question)
try:
return int(response)
except ValueError:
print("\nInvalid input! Input must be an integer")
else:
while True:
response = raw_input(question)
try:
return int(response)
except ValueError:
print("\nInvalid input! Input must be an integer")
def valid_int(question):
"""Creates a loop until an integer is entered"""
py_version = find_version()
if py_version == 3:
while True:
response = input("Length of password: ")
if response.isdigit()
return int(response)
print("\nInvalid input! Input must be an integer")
else:
while True:
response = raw_input("Length of password: ")
if response.isdigit()
return int(response)
print("\nInvalid input! Input must be an integer")