Feb-28-2020, 01:56 PM
I've got a program/script that I want it to restart from scratch if an error occurs. The script to restart the code looks like this:
The code runs fine until the restart line is executed; The output looks like this when the code restarts, as expected:
Accompanying screenshot below.
Some advice to fix this would be appreciated.
Thanks
1 2 3 4 5 6 7 8 9 10 11 12 |
import os import sys def divide(a,b): try : return a / b except ZeroDivisionError: print ( "Cannot divide by zero. Restarting. " ) os.execl(sys.executable, sys.executable, * sys.argv) a = int ( input ( "Please enter first number. " )) b = int ( input ( "Please enter second number. " )) divide(a,b) |
Output:PS D:\python> python testexit.py
Please enter first number. 5
Please enter second number. 0
Cannot divide by zero. Restarting.
PS D:\python> Please enter first number.
The problem happens when I tried to re-enter the first number (5) after the code restarted. I get an output that looks like this (line 5):Output:PS D:\python> python testexit.py
Please enter first number. 5
Please enter second number. 0
Cannot divide by zero. Restarting.
PS D:\python> 5lease enter first number.
Powershell then hangs (will not respond to further keyboard input) and I will have to close it and start over again. Accompanying screenshot below.
Some advice to fix this would be appreciated.
Thanks