Python Forum
Restarting code from scratch
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Restarting code from scratch
#1
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:

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)
The code runs fine until the restart line is executed; The output looks like this when the code restarts, as expected:
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

Attached Files

Thumbnail(s)
   
Reply
#2
It seems to me you are shooting at a tiny problem with heavy guns. To my opinion using "os.execl" for this occasion is not pythonic. On one hand you are right that an error should be fixed at the location where it occurs. So I wondered why I do not like this solution? It is because the error is not really solved.
So instead I propose to catch the error on a level where it can be retried. Like this:
def divide(a,b):
    return a/b
 
while True:
    a = int(input("Please enter first number. "))
    b = int(input("Please enter second number. "))
    try:
        print(divide(a,b))
        break
    except ZeroDivisionError:
        print("Cannot divide by zero. Restarting. ")
I hope you agree with this solution.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How do I properly implement restarting a multithreaded python application? MrFentazis 1 630 Jul-17-2023, 09:10 PM
Last Post: JamesSmith
  Skipping line in text without Restarting Loop IdMineThat 4 1,498 Apr-05-2022, 04:23 AM
Last Post: deanhystad
Big Grin New to python, starting from scratch Pythnoobvent1971 1 2,020 Apr-16-2021, 01:33 PM
Last Post: Yoriz
  [split] Kera Getting errors when following code example Image classification from scratch hobbyist 3 4,649 Apr-13-2021, 01:26 PM
Last Post: amirian
  Restarting a script new2codez 0 1,135 Nov-11-2020, 11:02 AM
Last Post: new2codez
  sqlite3 database does not save data across restarting the program SheeppOSU 1 3,456 Jul-24-2020, 05:53 AM
Last Post: SheeppOSU
  py 2.7 restarting the program Jonathan_levy 1 2,315 Jul-06-2018, 07:44 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020