I'm learning python, and am stuck on a project. The main part of the project was to code the collatz sequence, which wasn't a problem. The next part is to validate the user input using try and except, in order to make sure only an integer value is given in. So far, I have the following code:
def collatz(number):
if number % 2 == 0:
return number // 2
elif number % 2 == 1:
return (3*number) + 1
print('Please enter a number:')
number = ''
try:
number = int(input())
except ValueError:
print('Incorrect type of data entered.\nPlease enter an integer!')
while number is not int:
try:
number = int(input())
except ValueError:
print('Incorrect type of data entered.\nPlease enter an integer!')
output = number
while output != 1:
output = collatz(output)
print(output)
My problem is I'm not sure how to repeat the try/except statement until I get an integer from the user. Right now, if I enter a string instead of an integer, the program goes into a loop and entering an integer subsequently does not help. I read quite a few threads on the topic, but they didn't shed light as to the aforementioned problem.
Would really like to understand where I'm going wrong.