-2
if option == 'C':
    radius = float(raw_input("Enter Radius: ")) 
    area = pi * radius**2
    print "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*height
    print "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.

Vikash Singh
  • 13,213
  • 8
  • 40
  • 70
Patrick
  • 1
  • 1
  • Put it in a `while loop`... – MooingRawr Jun 30 '17 at 17:18
  • 3
    Related reading: [Asking the user for input until they give a valid response](https://stackoverflow.com/q/23294658/953482) – Kevin Jun 30 '17 at 17:19
  • place it in a function and you can call that function at the end – Stack Jun 30 '17 at 17:19
  • How though? I'm new to Python, I don't know where to put it. – Patrick Jun 30 '17 at 17:19
  • @Stack, could you please elaborate? Thanks. – Patrick Jun 30 '17 at 17:20
  • @Patrick Do you understand how to use a while loop? – Carcigenicate Jun 30 '17 at 17:22
  • @Carcigenicate, no I'm new to Python. Could you help? Thanks – Patrick Jun 30 '17 at 17:23
  • @Patrick Really, you should read a tutorial on loops first. This really isn't the best place to ask for people to teach you the basics of the language. Good tutorials on looping exist everywhere. If you have a problem understanding them, asking about what specifically you don't understand would be a good question. Programming is about problem solving and finding answers. – Carcigenicate Jun 30 '17 at 17:29
  • @Patrick SO is not a code creation service. Our job is to help you if you have a specific problem, not to write code for you if you are stuck. https://www.tutorialspoint.com/python/python_while_loop.htm https://wiki.python.org/moin/WhileLoop Check out these great tutorials on Python while loops. – victor Jun 30 '17 at 17:29
  • Possible duplicate of [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – mkrieger1 Jun 30 '17 at 17:33

6 Answers6

1

You should search online first before you ask any questions. Do you study the tutorial? It is clear that you already identify the question (how to restart it). Ask yourself how to do? It is like a loop. To create a loop, you need "while". Then how to meet this in order to loop the process? let the 'while' conditional be true all the time.

from time import sleep

pi = 3.1415926
hint = 'what' 

def function(option):

    if option == 'C':
        radius = float(input("Enter Radius: "))
        area = pi * radius ** 2
        print("Working...")
        sleep(1)
        print("Area: %.2f. \n%s" % (area, hint))

    elif option == 'T':
        base = float(input("Enter base: "))
        height = float(input("Enter height: "))
        area = (0.5) * base * height
        print("Working...")
        sleep(1)
        print("Area: %.2f. \n%s" % (area, hint))
    else:
        print("Error: Could not process. The program will restart.")
        sleep(4.5)

while True: # here is how you restart the program. it is just like C
    option = input("\nwhat do you want ? 'C' or 'T'\n")
    function(option)
Mr_U4913
  • 1,294
  • 8
  • 12
  • how do you exit your infinite while loop ?? – Vikash Singh Jun 30 '17 at 17:38
  • In this case, store a True into a variable like loop = True outside of the while loop (instead of while True, we do while loop). ask user to decide whether to loop or not inside of the while loop (loop = True / False) If use's input is False, the while loop will end. – Mr_U4913 Jun 30 '17 at 17:42
  • Add an option to exit, see my answer below. And adding a exception handler should be better – Fogmoon Jun 30 '17 at 17:44
  • 1
    There are lots of way to optimize the function or the program, what we need to do is to answer his/her question. We can do it better, but why don't we leave him/her room and make him/her to think and optimize it. – Mr_U4913 Jun 30 '17 at 17:46
  • @Mr_U4913 Not for argument, so why in `stackoverflow` need to choose a best answer (I do not mean my answer is best), just leave the requester to optimize is OK. In my option, answer a question, try my best give a best answer in my ability is the first thing. As regards to the optimizing room, I think it always exist. :-) – Fogmoon Jul 01 '17 at 11:13
  • That is true. BUT this is question is so basic, you can find many solutions online without asking question HERE. – Mr_U4913 Jul 03 '17 at 06:05
1

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**2
        print "Working..."
        sleep(1)
        print ("Area: %.2f. \n%s" % (area, hint))
        break
    elif option == 'T':
        base = float(raw_input("Enter base: "))
        height = float(raw_input("Enter height: "))
        area = (0.5)*base*height
        print "Working..."
        sleep(1)
        print ("Area: %.2f. \n%s" % (area, hint))
        break
    else:
        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.

double_j
  • 1,636
  • 1
  • 18
  • 27
0

There are better ways to write this code. But to simply fix your problem, put your code in a function and call the function. If function is a success return True, else Return False.

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

# When function return false, call the function again.
success = False
while not success:
    success = my_fuction()
Vikash Singh
  • 13,213
  • 8
  • 40
  • 70
0

A simple runnable demo with handling invalid input and option to break:

from time import sleep
from math import pi

hint = 'your hint'

while True:
    option = raw_input("Enter option: ")

    try:
        if option == 'C':
            radius = float(raw_input("Enter Radius: "))
            area = pi * radius ** 2
            print "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 * height
            print "Working..."
            sleep(1)
            print ("Area: %.2f. \n%s" % (area, hint))

        elif option == 'N':
            break

        else:
            print "Error: Could not process. The program will restart."
            sleep(4.5)

    except ValueError:
        print "Error: Could not process. The program will restart."
        sleep(4.5)

Hope this help.

Fogmoon
  • 569
  • 5
  • 16
0
#is this better
put = ""
while put != "yes":
    if option == 'C':
        radius = float(raw_input("Enter Radius: ")) 
        area = pi * radius**2
        print "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*height
        print "Working..."
        sleep(1)
        print ("Area: %.2f. \n%s" % (area, hint))
    else:
        print "Error: Could not process. The program will restart."
        put = "no"
        sleep(4.5)
-1

here is the code.

enter code here
def func():
    if option == 'C':
        radius = float(raw_input("Enter Radius: ")) 
        area = pi * radius**2
        print "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*height
        print "Working..."
        sleep(1)
        print ("Area: %.2f. \n%s" % (area, hint))
    else:
        print "Error: Could not process. The program will restart."
        func()#place it where ever you want in else part
        sleep(4.5)
  • 1
    I'll say it again, using recursion for something like this is far from optimal since python doesn't optimize recursion. If some dip enters the wrong character too many times, the program will crash with a SO, which is kind of a ridiculous restraint. I didn't downvote, but I don't get why recursion would be the go-to looping tool here. – Carcigenicate Jun 30 '17 at 17:34