-1

I just started learning python (please don't judge my code) and am confused about why I am receiving a syntax error titled, "'return' outside function." Could someone please help me? Also, if there are any other bugs, could you help me sort them out? Thank you!

Here is my code:

print("Hello! Welcome to Your Personal Geometric Calculator!")

choice = print("Would you Like to Calculate Area (A), Perimeter (P), or Volume (V)")

π = 3.14159

#Area

if choice == "A" or choice == "a":

    shape = input("What Shape do you Want to Find the Area of?:")

if shape == "Circle" or shape == "circle":

    rC = int(input("Enter the Radius of the Circle (Use Meters):"))
              
    print("The Area of the Circle:", π * (rC * rC), "m^2")

elif shape == "Square" or shape == "square":
                  
    sL = int(input("Enter the Length of the Side of the Square (Use Meters):"))
                  
    print("Area of the Sqaure:", sL * sL, "m^2")     

elif shape == "Rectangle" or shape == "rectangle":

    lR = int(input("Enter the Length of the Rectangle (Use Meters):"))
    
    wR = int(input("Enter the Width of the Rectangle:"))
    
    print("The Area of the Rectangle:", lR * wR, "m")

#Perimeter

elif choice == "P" or choice == "p":

    shape2 = ("What Shape do you Want to Find the Perimeter of?:")

if shape2 == "Circle" or shape2 == "circle":

    rC2 = int(input("Enter the Radius of the Circle (Use Meters):"))
              
    print("The Perimeter of the Circle:", 2 * (π * rC2), "m^2")

elif shape2 == "Square" or shape2 == "square":
                  
    sL2 = int(input("Enter the Length of the Side of the Square (Use Meters):"))
                  
    print("Perimeter of the Sqaure:", 4 * sL2, "m^2")

elif shape2 == "Rectangle" or shape2 == "rectangle":

    lR2 = int(input("Enter the Length of the Rectangle (Use Meters):"))
    
    wR2 = int(input("Enter the Width of the Rectangle:"))
    
    print("The Perimeter of the Rectangle:", 2 * (lR2 + wR2), "m")

#Volume

elif choice == "V" or choice == "v":

    shape3 = input("What Shape do you Want to Find the Volume of?:")

if shape3 == "Sphere" or shape3 == "sphere":

    rC3 = int(input("Enter the Radius of the Sphere (Use Meters):"))
              
    print("The Volume of the Sphere:", (4 * π * rC3 ** 3) / 3, "m^2")

elif shape3 == "Cube" or shape3 == "cube":
                  
    sL3 = int(input("Enter one Side Length of the Cube (Use Meters):"))

    print("The Volume of the Cube:", sL3 ** 3, "m^3")

elif shape3 == "Prism" or shape3 == "prism":

    lP = int(input("Enter the Length of the Prism (Use Meters):"))
    
    wP = int(input("Enter the Width of the Prism:"))

    hP = int(input("Enter the Height of the Prism:"))
    
    print("The Volume of the Prism:", lP * wR * hP, "m^3")

else:
    
    print("I'm sorry. We do not Support That Shape. Press Enter to try Again.")
    
    return choice
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
5HVRMVN
  • 1
  • 2
  • Return must be inside a function, so that when you call that function, it quite literally returns that value. Unless you have a function(s), the `return` is the wrong choice of syntax. – Larry the Llama Nov 15 '21 at 01:44
  • How do I allow the user to return back to "choice"? Thank you. – 5HVRMVN Nov 15 '21 at 01:45
  • Return to what? – chepner Nov 15 '21 at 01:46
  • If the user does not enter a correct shape, then I would like to return them to the beginning at the variable "choice" so they can start over. How should I do this? – 5HVRMVN Nov 15 '21 at 01:48
  • 1
    That's a job for a loop, not `return`. `return` means "end the current function execution, and optionally communicate an object back to the caller". – user2357112 Nov 15 '21 at 01:50
  • Welcome to Stack Overflow. Please read [ask] and https://meta.stackoverflow.com/questions/284236, and note that this is *not a discussion forum*. You are expected to ask a *single, focused* question at a time - we don't just look over chunks of code and debug any issue we can find. Please also read the [formatting help](https://stackoverflow.com/help/formatting) and make sure you understand how to post code properly. – Karl Knechtel Nov 15 '21 at 02:24
  • As to your main question, it's a bit difficult to answer you because it isn't clear *what you think `return` does or what it is for*, so we can't correct your understanding. That said, your level of experience is *not relevant* to asking the question, and you should understand that going around telling people that you're new is counterproductive. They can tell, and bringing it up a) comes across as a sympathy ploy; b) mentally conditions you not to improve. – Karl Knechtel Nov 15 '21 at 02:26
  • Instead of trying to design your own code and ask questions about how to do things (in ways that are difficult for others to understand, because you don't yet understand the terms for what you're trying to do), you should *follow a tutorial* and learn according to a set lesson plan. Yes, it feels stifling. Yes, it is necessary. *Every* new field of study is like this. You can't learn to play chess if you don't have names for the pieces or understand what moves they can legally make, so at the start you have to follow a step-by-step guide for that. – Karl Knechtel Nov 15 '21 at 02:28

1 Answers1

1
running = True
errors = False
while running:
    # Ask question
    
    # Answer, content, secondary-questions etc.
    
    # Code ran without problems, do not repeat
    if not errors: # invalid etc
        errors = False
        running = False # Done. No need to ask question again

If at any stage the user does not give an answer you like, you can just set errors to True.

Note that this code can be modified, and the locations of where you are answering your questions can be changed. Also, this is a little janky, but will work when correctly modified to your situation.

Larry the Llama
  • 958
  • 3
  • 13