def get_int(prompt="Enter an Integer:"):
while True:
try:
return int(input(prompt))
except ValueError:
print("That is not an integer!")
x = get_int("What is 1+1")
def get_integer(): # Define a function to make things easier
while True: # Keep looping, until we return from the function
x = input('What is 1 + 1?') # ask for input
try:
x = int(x) # can we convert it to an integer?
except (ValueError, TypeError): # None or 'hello' would fail
print('Invalid entry, please try again!')
else: # no exception!
print('x was good!')
return x # exit our function
value = get_integer() # Call our function
相关问题 更多 >
编程相关推荐