#Troubleshoot: Collatz Conjecture

9 messages · Page 1 of 1 (latest)

sharp thunder
#

Hi all,

I've been trying to undertake the Collatz Conjecture exercise (Python track -> Numbers concept exercise) and I'm struggling. Below is what I've written so far, I'd be grateful of any pointers of where I'm going wrong.

def steps(number):
    if number == 0:
        raise ValueError("Number cannot be 0")
        if number < 0:
            raise ValueError("Number cannot be negative")
            if number == 1:
                print("Count equals 0")
            else:
                count = 0    
                while number != 1:
                    if number % 2 == 0:
                        number = number / 2
                        count += 1
                    else:
                        if number % 2 == 1:
                            number = (number * 3) + 1
                            count += 1
                return count

Thanks,

Snook

tough whale
#

Please use code block and give all the relevant information like which track (programming language) is this from

#

Increase your chance of getting help and look like a pro by sharing codeblocks not images.
Click here to learn more about codeblocks: https://exercism.org/docs/community/being-a-good-community-member/writing-support-requests and http://bit.ly/howto-ask

Exercism

Want help? Help yourself by learning how to write a good support request!

Do you often get ignored when you ask people online to help you with your coding problems? Make sure you're not breaking any of those 7 rules.

sharp thunder
#

Thanks glaxxie, I've amended my post.

tough whale
#

have you done all the easier/ syllabus for the track yet? all the basic early exercise?

sharp thunder
#

Yeah, I'm completing it in order.

tough whale
#

then you do understand the important of white space and indentation in python?

#

does your code do anything if the number is not equal to 0?

sharp thunder
#

I do, but I don't always find it intuitive as to whether I should indent something or not. Hopefully that'll come with time.

Good news, I've cracked it. Your question about what happens if the number is not equal to zero got the cogs turning. This is what I've amended it to:


def steps(number):
    if number == 0:
        raise ValueError("Only positive integers are allowed")
    else:
        if number < 0:
            raise ValueError("Only positive integers are allowed")
        else:
            if number == 1:
                return 0
            else:
                count = 0    
                while number != 1:
                    if number % 2 == 0:
                        number = number / 2
                        count += 1
                    else:
                        if number % 2 == 1:
                            number = (number * 3) + 1
                            count += 1
                return count

Thanks for the assistance, it's much appreciated. We journey on...👍

Snook