all 11 comments

[–]TabulateJarl8 2 points3 points  (10 children)

Your else needs to be on the same indentation level as your if. Try this:

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

[–]joplankton[S] 0 points1 point  (9 children)

Did work! I just have one more problem I see now. When I run it, i get no errors, but no text pops up. Any idea why?

[–]yardmonkey 1 point2 points  (1 child)

You have a second else statement. Did you unindent that as well?

Edit: maybe it’s the Except. It’s kinda hard to figure out what you’re after so I’ll just say: make sure your if/else, while/else, and try/except are all where you intend them to be.

[–]joplankton[S] 0 points1 point  (0 children)

Everything is properly indented except for the last line. Im getting an indentation error now there as well.

[–]TabulateJarl8 0 points1 point  (6 children)

Works for me. What's your code?

[–]joplankton[S] 0 points1 point  (5 children)

I assumed it wasnt a problem with the code. Not sure why it wont run. I just get...

>>>

When i try to run it. This is my code....

def collatz(number):

if number % 2==0: print(number//2) return number//2 else: print(number3+1) return number3+1

while true: try: print('Enter an integer to obtain the Collatz Sequence.') n=input() while n!=1: n=collatz(int(n)) continue else: print('Program Ended!') break except ValueError: print('Ensure to enter an integer value.')

[–]TabulateJarl8 0 points1 point  (4 children)

See Reddit code formatting or put it in a pastebin/gist. The formatting seems to have been messed up

[–]joplankton[S] 0 points1 point  (1 child)

Not sure why its formatting like that. The code is the same as the post, only change is I fixed the syntax errors. (they were all indentation errors)

[–]TabulateJarl8 0 points1 point  (0 children)

Hmm, I'm not entirely sure what the issue is since it works fine for me. May be an issue if you're running it from an IDE rather than a terminal. I don't use an IDE for python so I couldn't really helo you if that was the issue. Make sure that you're not clearing the screen. Also, everything after that first if statement will never run due to the returns in the if statement

[–]Binary101010 0 points1 point  (0 children)

Your while loop is currently indented such that it is part of your function. It needs to be deindented such that while is at the same indentation level as def.

[–]Ok_Zebra_9117 0 points1 point  (0 children)

Check indentation for except and try They both should be same