Collatz Loop Structure


Collatz Loop Structure



Please help me understand what i did wrong. The problem is



The Collatz Sequence
Write a function named collatz() that has one parameter named number. If number is even, then collatz() should print number // 2 and return this value. If number is odd, then collatz() should print and return 3 * number + 1.



Then write a program that lets the user type in an integer and that keeps calling collatz() on that number until the function returns the value 1. (Amazingly enough, this sequence actually works for any integer—sooner or later, using this sequence, you’ll arrive at 1! Even mathematicians aren’t sure why. Your program is exploring what’s called the Collatz sequence, sometimes called “the simplest impossible math problem.”)



Remember to convert the return value from input() to an integer with the int() function; otherwise, it will be a string value.



Hint: An integer number is even if number % 2 == 0, and it’s odd if number % 2 == 1.


def collatz(number):
if number%2==0:
print(number//2)
else:
print(3*number+1)

x = int(input('print num'))
while TRUE:
if collatz(x)!=1:
break





What makes you think you did something wrong? Tell us. We're not mind readers.
– jwodder
Jun 30 at 2:09





You need to return the result instead of printing it. Capture that return value in a variable and then use it as the argument to your next call to collatz
– Patrick Haugh
Jun 30 at 2:14


return


collatz





You need to fix your indentation, what you posted is not legal. Also, it would help you debug your own error if you printed number on every iteration inside the loop.
– smci
Jun 30 at 2:14


number





The indentation is wrong, you must print and return the value, you are not capturing the result of calling the function...
– David Conrad
Jun 30 at 2:21





Possible duplicate of Making a collatz program automate the boring stuff
– Jetstream5500
Jun 30 at 3:45




2 Answers
2



You must print and return in the function collatz(num):


collatz(num)


def collatz(number):
"""prints and returns the next number in the Collatz sequence
"""
if number % 2 == 0:
next_collatz_number = number // 2
else:
next_collatz_number = 3 * number + 1
print(next_collatz_number)
return next_collatz_number

x = int(input('print num'))

while True:
x = collatz(x)
if x == 1:
break





Indeed, thank you for pointing this out @PM2Ring
– Reblochon Masque
Jun 30 at 3:16



Duplicate - Making a collatz program automate the boring stuff



As others have stated in the comments, your function collatz() must return an integer also, to be fed into collatz() again.


collatz()


collatz()



Building off of what you have already done, we can have the function collatz_sequence(x) repeatedly call collatz() to get the desired result:


collatz_sequence(x)


collatz()


def collatz(x):
if x % 2 == 0:
a = x//2
else:
a = 3*x+1
print(a)
return a

def collatz_sequence(x):
print(x)
while x!=1:
x=collatz(x)



Here is an example output:


>>> collatz_sequence(6)
6
3
10
5
16
8
4
2
1






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

how to run turtle graphics in Colaboratory

Export result set on Dbeaver to CSV