Home > Software engineering >  How to loop after 'Except ERROR' line in Python
How to loop after 'Except ERROR' line in Python

Time:06-14

as part of the Chapter 3 exercise for Automate the Boring Stuff, I need to write a short program that mimics the Collatz sequence, where:

  • If number inputted is even number, divide it by 2, repeat until it equals 1;
  • If number inputted is odd number, multiply it by 3 then plus 1, repeat until it equals 1;
  • Create a clean exit for Ctrl C.
  • Detect whether the user typed in a noninteger string.

Below is my code so far, which seems to work but I would appreciate any advice/best practice for improving it.

My main question is, after the program prints 'Enter integers only', is there any short and simple way to loop back to the 'Enter any number: ' line? I can't think of anything atm besides complicated if loops. Thanks.

def collatz(number):
    if number % 2 == 0 :
        results = number // 2
        print(results)
        if results != 1:
            collatz(results)
    elif number % 2 == 1 :
        results = 3 * number   1
        print(results)
        if results != 1:
            collatz(results)

try: 
    entry = input('Enter any number : ')
    number = int(entry)
    print(number)
    collatz(number)
except ValueError:
    print('Enter integers only.')
except KeyboardInterrupt:
    sys.end()

CodePudding user response:

You can try using a while loop. The below code will be helpful for you.

def collatz(number):
    if number % 2 == 0 :
        results = number // 2
        print(results)
        if results != 1:
            collatz(results)
    elif number % 2 == 1 :
        results = 3 * number   1
        print(results)
        if results != 1:
            collatz(results)

while True:
    try: 
        entry = input('Enter any number : ')
        number = int(entry)
        print(number)
        collatz(number)
    except ValueError:
        print('Enter integers only.')
    except KeyboardInterrupt:
        break # Stop the while loop.
        sys.end()

CodePudding user response:

As you want to keep prompting the same, until the program exits. I'd suggest just encapsulating the program code in a while(true) loop:

while True:
    try: 
        entry = input('Enter any number : ')
        number = int(entry)
        print(number)
        collatz(number)
    except ValueError:
        print('Enter integers only.')
    except KeyboardInterrupt:
        sys.end()

CodePudding user response:

You can define an iteration to make sure the input is integer type.

while True: # executes the loop body indefinitely
    try: 
        entry = input('Enter any number : ')
        number = int(entry)
        print(number)
        collatz(number)
        break  # immediately terminates the loop entirely
    except ValueError:
        print('Enter integers only.')
    except KeyboardInterrupt:
        sys.end()

CodePudding user response:

Put it in a "while" loop.

while True:
    try:
        ...
    except:
        ...
  • Related