0

I'm learning python and just started using automate the boring stuff to learn.

This is the code I came up for collatz sequence. I don't know if it's good because if I don't enter an integer, the program finishes and I guess it's supposed to restart the loop so I can use an integer and I don't know how to do that.

This is the code:

def collatz(number):

 if number % 2 == 0:
   return number // 2

 elif number % 2 == 1:
    return 3*number +1

print("Insert a number")

 try:
  number = int(input())
 while number != 1:
  number = collatz(number)
  print (number)


except ValueError:
 print ("Use an integrer")
quamrana
  • 37,849
  • 12
  • 53
  • 71
tigron123
  • 25
  • 1
  • 7
    @ggorlen I disagree. Trying to use this code as-is results in IndentationError exceptions to be thrown, and OP indicates that the program is probably supposed to restart a loop but doesn't, which means it's not working code. The CR site is for **working complete code**, not code where the OP is asking for help fixing an issue, and especially not for code that errors when you try to run it as is. As such, it doesn't qualify for the CR site. – Thomas Ward Aug 02 '18 at 14:57
  • https://pastebin.com/pGQmUxv0 – whackamadoodle3000 Aug 02 '18 at 14:59
  • @ThomasWard I agree on second look, but the way the question is framed is "is this good" (CR) rather than "fix my problem" (SO). The answer to "is this good" may be "no". – ggorlen Aug 02 '18 at 15:00
  • 1
    @ggorlen That was my first-glance impression as well, but moderating over at Ask Ubuntu has taught me to not go by 'first glance' so I always look at posts slightly more in-depth to try and determine what actually is being asked/requested. And at least for CR, I look for certain key phrases which would make it not on topic there :P – Thomas Ward Aug 02 '18 at 15:01
  • Btw: you might as well replace the `elif` in `collatz()` with `else` since you really only have 2 cases: the if-condition checks for even numbers everything else is odd. – meissner_ Aug 02 '18 at 15:03

0 Answers0