How do I restart my program in Python? (see code)

2024/7/8 6:20:09
if option == 'C':radius = float(raw_input("Enter Radius: ")) area = pi * radius**2print "Working..."sleep(1)print ("Area: %.2f. \n%s" % (area, hint))elif option == 'T':base = float(raw_input("Enter base: "))height = float(raw_input("Enter height: "))area = (0.5)*base*heightprint "Working..."sleep(1)print ("Area: %.2f. \n%s" % (area, hint))
else:print "Error: Could not process. The program will restart."sleep(4.5)

I want the code to restart at the bottom, only if the user types something wrong- underneath 'else.' I don't want it to ask them to restart, but automatically. Thanks!!

Also, please show it, not tell. I need to see it, I'm new to Python.

Answer

Wrapping your code in a loop will create a "restart" feature you want, i.e.

while True:option = raw_input()if option == 'C':radius = float(raw_input("Enter Radius: "))area = pi * radius**2print "Working..."sleep(1)print ("Area: %.2f. \n%s" % (area, hint))breakelif option == 'T':base = float(raw_input("Enter base: "))height = float(raw_input("Enter height: "))area = (0.5)*base*heightprint "Working..."sleep(1)print ("Area: %.2f. \n%s" % (area, hint))breakelse:print "Error: Could not process. The program will restart."sleep(4.5)

This will end the loop if one of the options was selected, otherwise it will restart from the beginning.

https://en.xdnf.cn/q/119923.html

Related Q&A

how to create a list of elements from an XML file in python

my XML <root> - <Book category="Children"><title>Harry Potter</title> <author>J.K</author> <year>2005</year> <price>29.99</price> &…

Is random.sample truly random?

I have a list with 155k files. When I random.sample(list, 100), while the results are not the same from the previous sample, they look similar. Is there a better alternative to random.sample that retur…

how to extract a table column data present in pdf and stored inside a variable python

I have 3 tables (image pasted) all 3 table(have same columns) look same and i want data of address column (yellow colour) of 3 tables stored inside a variable.

Pong Created in Python Turtle

Im new to Python but Ive coded in other languages, mainly for hardware. I made pong in Python using turtle but its a little glitchy. I was wondering if any of you could check it out and give advice. I …

How to build a Neural Network with sentence embeding concatenated to pre-trained CNN

I want to build a neural network that will take the feature map from the last layer of a CNN (VGG or resnet for example), concatenate an additional vector (for example , 1X768 bert vector) , and re-tra…

Obtaining values from columns in python

I want to obtain the top 3 cities and items based on their sales, but the only thing I can do now is return the all cities and items with their respective sales. Without using dict, can I obtain my des…

Is there a really efficient (FAST) way to read large text files in python?

I am looking to open and fetch data from a large text file in python as fast as possible (It almost has 62603143 lines - size 550MB). As I dont want to stress my computer, I am doing it by following wa…

How to extract all K*K submatrix of matrix with or without NumPy?

This is my input: row=6 col=9 6 9 s b k g s y w g f r g y e q j j a s s m s a s z s l e u s q u e h s s s g s f h s s e s g x d r h g y s s sThis is my code: r=int(input()) c=int(input()) n=min(r,c) k=…

How to scrape multiple result having same tags and class

My code is accurate for single page but when I run this code for multiple records using for loop and if there are some data missing like person then (as I used index no[1] and [2] for person variable ,…

Is there an alternative for sys.exit() in python?

try:x="blaabla"y="nnlfa" if x!=y:sys.exit()else:print("Error!") except Exception:print(Exception)Im not asking about why it is throwing an error. I know that it raises e…