Python 第一个应用程序错误缩进错误:应为缩进块
我刚开始学习python,我做了一个简单的计算器,我试图让它重新启动,但我得到了这个错误Python 第一个应用程序错误缩进错误:应为缩进块,python,Python,我刚开始学习python,我做了一个简单的计算器,我试图让它重新启动,但我得到了这个错误 > F:\newb\venv\Scripts\python.exe F:/newb/python.py File > "F:/newb/python.py", line 4 > what = input("Choose your operation:") > ^ IndentationError: expected an indented block >
> F:\newb\venv\Scripts\python.exe F:/newb/python.py File
> "F:/newb/python.py", line 4
> what = input("Choose your operation:")
> ^ IndentationError: expected an indented block
>
> Process finished with exit code 1
我的代码:
# Calculator
def main():
what = input("Choose your operation:")
number = float( input("Whats ur number 1?") )
number2 = float( input("Whats ur number 2?") )
if what == "+":
result = number + number2
print("Result: " + str(result))
elif what == "-":
result = number - number2
print("Result: " + str(result))
elif what == "*":
result = number * number2
print("Result: " + str(result))
elif what == "/":
result = number / number2
print("Result: " + str(result))
else:
print(" Choose a correct operation ")
restart = input("do you wish to start again?").lower()
if restart == "yes":
main()
else:
exit()
在我添加
defmain():
和Python底部的重新启动代码之前,它已经工作了,缩进就像其他语言中的括号一样。为了标记代码块,整个代码块需要缩进,因此在Python中,与大多数其他语言不同,空格很重要。如果你想了解更多,请阅读
如果您希望了解如何缩进主函数,我已在下面的语句中修复了您的代码:
def main():
# You need to everything that belongs to the main function indent here, like so!
what = input("Choose your operation:")
number = float( input("Whats ur number 1?") )
number2 = float( input("Whats ur number 2?") )
if what == "+":
result = number + number2
print("Result: " + str(result))
elif what == "-":
result = number - number2
print("Result: " + str(result))
elif what == "*":
result = number * number2
print("Result: " + str(result))
elif what == "/":
result = number / number2
print("Result: " + str(result))
else:
print(" Choose a correct operation ")
# notice we de-indent here, so that we end the main function.
# add in a main line to start the code once
main()
restart = input("do you wish to start again?").lower()
if restart == "yes":
main()
else:
exit()
尽管我建议使用以下方式创建“main”函数(参考:
在Python中,缩进类似于其他语言中的括号。要标记代码块,整个代码块都需要缩进,因此在Python中,与大多数其他语言不同,空格很重要。如果您想了解更多信息,请阅读 如果您希望了解如何缩进主函数,我已在下面的语句中修复了您的代码:
def main():
# You need to everything that belongs to the main function indent here, like so!
what = input("Choose your operation:")
number = float( input("Whats ur number 1?") )
number2 = float( input("Whats ur number 2?") )
if what == "+":
result = number + number2
print("Result: " + str(result))
elif what == "-":
result = number - number2
print("Result: " + str(result))
elif what == "*":
result = number * number2
print("Result: " + str(result))
elif what == "/":
result = number / number2
print("Result: " + str(result))
else:
print(" Choose a correct operation ")
# notice we de-indent here, so that we end the main function.
# add in a main line to start the code once
main()
restart = input("do you wish to start again?").lower()
if restart == "yes":
main()
else:
exit()
尽管我建议使用以下方式创建“main”函数(参考:
你需要缩进你想在主函数中出现的所有内容。重新读取错误,在得到错误之前不要停止。Python使用缩进来构造它的代码块。你需要使用四个空格或一个制表符(取决于你的系统)缩进函数或循环中的每一行。你需要缩进你想在主函数中出现的所有内容。重读错误,在得到错误之前不要停止。Python使用缩进来构造代码块。你需要使用四个空格或一个制表符(取决于你的系统)缩进函数或循环中的每一行。编辑以添加关于如何在PythonEdited中生成“main”函数的最佳实践添加关于如何在Python中生成“main”函数的最佳实践添加