I am currently trying to learn Python from a book and I am running into a problem. Basically I am supposed to write a program to do something called the Collatz sequence. The code is as follows:
print("Please enter a number")
number = input()
int(number)
while number > 1:
collatz()
def collatz(number):
if number % 2 == 0:
number = number // 2
print(number)
return number
else:
number = 3 * number + 1
print(number)
return number
So when I try to execute the code above, I get the following output:
RESTART: C:/Users/Gillian/AppData/Local/Programs/Python/Python35-32/Collatz.py
Please enter a number
12
Traceback (most recent call last): File "C:/Users/Gillian/AppData/Local/Programs/Python/Python35-32/Collatz.py", line 6, in
while number > 1:
TypeError: unorderable types: str() > int() Blockquote
Obviously, my variable is being read as a string when it should be an integer. My question is - why? I thought that the part on line 4 where I change the datatype of number to int should fix that but it did not.
The instructions for what I'm trying to accomplish, if that matters to my question, are here at the bottom of the page.