'return' 外部函数语法错误
'return' outside function SyntaxError
我刚开始学习 python(请不要评判我的代码),我很困惑为什么我会收到标题为“'return' 外部函数”的语法错误。有人可以帮我吗?另外,如果还有其他bug,能帮我解决一下吗?谢谢!
这是我的代码:
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
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
如果在任何阶段用户没有给出您喜欢的答案,您可以将错误设置为True
。
请注意,此代码可以修改,您回答问题的位置也可以更改。此外,这有点简陋,但在根据您的情况正确修改后会起作用。
我刚开始学习 python(请不要评判我的代码),我很困惑为什么我会收到标题为“'return' 外部函数”的语法错误。有人可以帮我吗?另外,如果还有其他bug,能帮我解决一下吗?谢谢!
这是我的代码:
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
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
如果在任何阶段用户没有给出您喜欢的答案,您可以将错误设置为True
。
请注意,此代码可以修改,您回答问题的位置也可以更改。此外,这有点简陋,但在根据您的情况正确修改后会起作用。