Fairly new to the Pythonic Way, but the below code doesn't work as expected, anyone know why? If I input 143.22 I get "not an int" response, rather than the expected collatz(143) e.g., int(143.22). BTW, this is one basic exercise in the Automate the Boring Stuff with Python beginners book (chapter 3 exercise on using functions). Thanks!
def collatz(myint) :
print(myint)
if myint % 2 == 0 :
myint = int(myint / 2)
else:
myint = int(myint *3 +1)
if myint == 1 :
print(myint)
return myint
else:
collatz(myint)
try:
x=input('Input a non-negative integer between 1 and infinity: ')
val = int(x)
if int(x) == abs(int(x)) :
print("Yes input string is an Integer.")
collatz(val)
else:
print('I did say NON negative!')
except ValueError:
print("That's not a positive int!")
val = int(x). You can applyint(143.22), but you are not allowed toint('143.22'). Read more docs.python.org/3.8/library/functions.html#intint(x) == abs(int(x))byval == abs(val).