Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我不知道';我不明白我的缩进为什么是错误的,当我输入答案时,它不会';除了一个错误,我什么也不输出_Python_Python 2.7 - Fatal编程技术网

Python 我不知道';我不明白我的缩进为什么是错误的,当我输入答案时,它不会';除了一个错误,我什么也不输出

Python 我不知道';我不明白我的缩进为什么是错误的,当我输入答案时,它不会';除了一个错误,我什么也不输出,python,python-2.7,Python,Python 2.7,我正在使用PyScripter w/Python2.7.6运行它。我不明白为什么我的代码错了。有人能给我解释一下吗 def mainMenu(): answer = input("""Main Menu: Pythagoras Repeat Program Quit""") if answer == "Pythagoras": pythagoras() elif answer == "Repeat Program": mainMenu() elif answer == "Quit

我正在使用PyScripter w/Python2.7.6运行它。我不明白为什么我的代码错了。有人能给我解释一下吗

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()

不确定粘贴时是否出现缩进错误,但这也是需要解决的问题

  • 在进入
    Pythagoras()
    函数之前,已经测试了
    if answer==“Pythagoras”
    ,在函数内部检查
    answer
    既不起作用也没有意义
  • 无法对
    字符串执行数学运算
    需要将输入转换为
    int
  • 在获取输入和打印结果时,为清晰起见,采用常规格式
  • PEP-8
    snake\u案例
    not
    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()

你知道你需要一个缩进后的
def
?是的,很抱歉它没有出现在这里,我是这个网站的新手。请给出一个包括你的实际缩进;它在Python中很重要。阅读编辑帮助。谢谢,对不起,你太笨了。@sambarringer,你没有傻。你是新来的。这是不同的。请回答您的问题,重新粘贴代码,然后选择它并按Ctrl+K或单击
{}
按钮缩进整个内容。这会保留你的缩进。
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