Home > Software design >  python code errors in while loop and if else
python code errors in while loop and if else

Time:01-05

I don't know why it is not working there are no errors or warnings it was suppossed to multiply by 3 and add 1 if the number in odd and if the number is even it is supposed to divide by 2 until it reaches 0 here is my code any help will be appreciated

    num = 4
while num != 0:
    if num % 2 == 1:
        num = (num * 3)   1
    else:
        num /= 2

CodePudding user response:

Your loop wil run indefinitely because num wil never be 0. I think you want num != 1. look at this wikipedia page https://en.wikipedia.org/wiki/Collatz_conjecture, And specificly the picture, you can see that 0 is never reached.

  •  Tags:  
  • Related