Home > front end >  How to take a new number and run the same process python
How to take a new number and run the same process python

Time:01-18

I'm trying to make a script that calculates the famous "3x 1" equation and I want python to take a number in by the user then determine if its even or odd. If even divide by half if odd do 3x 1 and then take the new number that went through the process and do it again until the number becomes 4. I have all the parts done and properly working except for the part that takes the new number and repeats the process. Does anyone know how i can get this to take the new number created and repeat the process. (for those wondering why i made the number of times it does the process meow i couldn't think of a name and my cat was next to me meowing for food so i went with it) Code:

meow = 0
num = int(input("Enter a number"))
meow  = 1
while True:
    if num == 4:
        print("you are now caught in a loop. (4 becomes 2 which becomes 1 which becomes 4 ect)")
        print("it took this number",meow-1,"times to get caught in this uninevitable loop for all recorded numbers")
    else:
        if(num % 2 != 0):
            print(num*3 1)
        else:
            print(num/2)

CodePudding user response:

Just update num every iteration:

meow = 0
num = int(input("Enter a number"))
meow  = 1
while True:
    if num == 4:
        print("you are now caught in a loop. (4 becomes 2 which becomes 1 which becomes 4 ect)")
        print("it took this number",meow-1,"times to get caught in this uninevitable loop for all recorded numbers")
    else:
        if(num % 2 != 0):
            num = num*3 1
            print(num)
        else:
            num = num/2
            print(num)
  •  Tags:  
  • Related