Search code examples
pythonrestart

How can I Restart Python code when I type a string


x = int(input("Type lenght: "))
y = int(input("Type Width: "))

print("IF you wanna find AREA Type Area")
print("IF you wanna find VOLUME Type Volume")

z = str(input("What You wanna find: "))

area = x * y
volume = (x * 2) + (y * 2)

if z == 'Area' or 'area':
    print("Area is ", area)
elif z == 'Volume' or 'volume':
    print("Volume is ", volume)
else:
    print("Something Goes Wrong")

print("You wanna find another value type RESTART")
print("You wanna find another value type CLOSE")

a = str(input())
if a == 'CLOSE' or 'close' or 'Close':
    exit()

This is one of my basic python code. if someone types restart, I want to make this code restart. Do you guys know how to do that


Solution

  • Make your code into a function. And make the user select part out of the function. Whenever want to restart. Just call the function.

    Here is a template:

    def myJobFn():
      x = int(input("Type length: "))
      # blah blah blah
    
    arg = "RESTART"
    while arg not in ["CLOSE"]:
      if arg == "RESTART":
        myJobFn()
      arg = input("Type 'RESTART' or 'CLOSE'").upper()
    

    Or just ignore RESTART option:

    arg = None
    while arg not in ["CLOSE"]:
      myJobFn()
      arg = input("Type 'CLOSE' for quit").upper()