Python新手几何计算器报'return' outside function错误求排查修复
问题排查与修复说明
1. 核心SyntaxError报错原因
return语句只能在Python的自定义函数内部使用,你当前代码是全局脚本逻辑,没有被def关键字定义的函数包裹,所以最后一行的return choice直接触发“'return' outside function”报错,重试逻辑可以通过循环实现,不需要使用return。
2. 其他现存Bug汇总
- 第3行变量赋值逻辑错误:
print函数返回值是None,获取用户输入应该用input而不是print,导致choice永远取不到用户选择的值 - 缩进逻辑错误:所有形状判断的
if/elif没有缩进在对应的choice判断分支下,会触发变量未定义报错(比如选择了周长计算,代码依然会去判断没赋值的shape变量) - 周长模块的
shape2赋值漏写input,直接把提示字符串赋值给了变量,无法获取用户输入的形状 - 单位错误:
- 矩形面积单位写错为
m,正确应为m^2 - 圆周长、正方形周长单位写错为
m^2,正确应为m - 球体积单位写错为
m^2,正确应为m^3
- 矩形面积单位写错为
- 棱柱体积计算错误:用到了未定义的
wR变量,实际应该使用当前分支定义的wP - 拼写错误:多次把
Square打印为拼写错误的Sqaure - 分支匹配错误:最后的
else只和棱柱判断的elif匹配,和最外层的计算类型选择分支无关,非法输入的提示逻辑完全失效
3. 修正后完整代码
print("Hello! Welcome to Your Personal Geometric Calculator!") π = 3.14159 # 外层循环实现重试逻辑 while True: choice = input("Would you Like to Calculate Area (A), Perimeter (P), or Volume (V):") # 面积计算分支 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 Square:", 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^2") else: print("I'm sorry. We do not Support That Shape.") # 周长计算分支 elif choice == "P" or choice == "p": shape2 = input("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") elif shape2 == "Square" or shape2 == "square": sL2 = int(input("Enter the Length of the Side of the Square (Use Meters):")) print("Perimeter of the Square:", 4 * sL2, "m") 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") else: print("I'm sorry. We do not Support That Shape.") # 体积计算分支 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^3") 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 * wP * hP, "m^3") else: print("I'm sorry. We do not Support That Shape.") else: print("Invalid option. Please select A, P or V.") # 重试/退出判断 retry = input("Press Enter to try again, or input Q to exit:") if retry.upper() == 'Q': break
内容的提问来源于stack exchange,提问作者5HVRMVN
相关产品推荐
相关产品推荐

