I have a function which works with integers. I have added code for exception handling if strings or floats are entered, however, Python keeps returning either nonsense or
"""isNotCorrect = True
if type(num) is not int:
print("Sorry, you need to enter a whole number to play Threes.")
isNotCorrect = False"""
while num > 1:
if num % 3 == 0:
num = num/3
if ((num + 1) % 3 == 0):
num = (num + 1)/3
num = (num - 1)/3
print("Invalid number. Please enter a valid number.")
Traceback (most recent call last):
File "<input>", line 1, in <module>
NameError: name 'bob' is not defined
NameError: name 'bob' is not defined means that you try to use a variable named
bob but you didn't create it.
You have to define what is
bob like that for exemple :
bob = 'a simple man' # bob is a string
bob = 4 # bob is an integer
Furthermore, I just want you to know that even if it works, try to avoid using try/except because this is quite slow when you have to enter in the except block.
If you want to check if your variable is an integer I recommend you using :
if isinstance(bob, int): # do something else: # do something else
Where isinstance() is a built-in function.