python - 我不明白为什么我的缩进是错误的,当我输入答案时,除了错误之外什么都不输出
问题描述
我正在使用带有 Python 2.7.6 的 PyScripter 运行它。我不明白为什么我的代码是错误的。有人可以给我一个解释吗?
def mainMenu():
answer = input("""Main Menu:
Pythagoras
Repeat Program
Quit""")
if answer == "Pythagoras":
pythagoras()
elif answer == "Repeat Program":
mainMenu()
elif answer == "Quit":
print("Program ended")
else:
mainMenu()
def pythagoras():
if answer == "Pythagoras":
aNotSquared = input("Input the value of A")
bNotSquared = input("Input the value of B")
aSquared = aNotSquared ** 2
bSquared = bNotSquared ** 2
valueC = aSquared + bSquared
print(valueC)
mainMenu()
解决方案
不确定粘贴时是否出现缩进错误,但在外部也有一些问题需要修复
if answer == 'Pythagoras'
在进入函数之前已经测试过,在函数内部pythagoras()
检查不起作用也没有意义answer
- 无法根据
strings
需要执行数学运算将您的输入转换为int
- 在输入和打印结果时,为清晰起见的通用格式
- PEP-8
snake_case
不CamelCase
略微改进的版本:
from math import sqrt
def main_menu():
answer = input("""Main Menu:
Pythagoras
Repeat Program
Quit\nChoose option from above: """)
if answer == "Pythagoras":
pythagoras()
elif answer == "Repeat Program":
main_menu()
elif answer == "Quit":
print("Program ended")
else:
main_menu()
def pythagoras():
a_not_sqr = int(input("Input the value of A: "))
b_not_sqr = int(input("Input the value of B: "))
a_sqr = a_not_sqr ** 2
b_sqr = b_not_sqr ** 2
c_sqr = a_sqr + b_sqr
c_not_sqr = sqrt(c_sqr)
print(f'C Squared = {c_sqr}')
print(f'C = {round(c_not_sqr, 2)}')
main_menu()
Main Menu: Pythagoras Repeat Program Quit Choose option from above: Pythagoras Input the value of A: 10 Input the value of B: 10 C Squared = 200 C = 14.14
推荐阅读
- php - "ereg_replace("\n","\\n",$row[$j])" 表达式有什么用?
- vue.js - 使用 WebStorm 将常规 JavaScript 文件标记为 Vue 组件
- kotlin - 如何正确合并两个地图?
- vue.js - Set-Cookie not accessible through axios or fetch
- amazon-web-services - CDK:模板格式错误:参数计数 63 大于允许的最大值 60
- sql - 在 MSSQL QUERY 中创建表时是否可以为列添加描述
- sql - 如何将 Informatica 的 Normalizer Transformation 转换为 SQL 查询?
- performance - 各种 Power BI 数据源的(相对)性能如何?
- parsing - Haskell:如何将语义空白集成到解析器中?
- oracle - 过程中的相同查询需要不同的时间