python-3.x 我不知道为什么我得到了一个缩进错误,我的功能

r6hnlfcb  于 4个月前  发布在  Python
关注(0)|答案(3)|浏览(88)

我在python 3中运行函数时遇到以下错误。
IndentationError:在第1行的函数定义后需要缩进块
这是一个非常简单的函数

def Calculate_total(a,b):   #Parameters: a and b
    total = a + b           #Task Addition
    return total            #Output Sum of a and b\

字符串

bxjv4tth

bxjv4tth1#

感谢所有回复的人。原来我只是运行了第一行代码,而不是后面的代码。我相信我的问题是在VS Code中使用了REPL终端,并试图在我的函数中突出显示一行代码,并假设剩余的代码块会运行。事实并非如此,相反,我需要突出显示整个代码块,然后运行函数。

xoshrz7s

xoshrz7s2#

一定有其他问题,就像注解中所建议的那样。我试着运行你的代码没有问题。请查看我的代码块并尝试那个。

def Calculate_total(a,b):   #Parameters: a and b
    total = a + b           #Task Addition
    return total            #Output Sum of a and b\

def main():
    print(Calculate_total(1,2))

if __name__ == "__main__":
    main()

字符串

dgtucam1

dgtucam13#

Python依赖于适当的缩进来定义代码块。请确保函数中的代码正确缩进。以下是代码的更正版本:

def Calculate_total(a, b):   # Parameters: a and b
    total = a + b            # Task: Addition
    return total             # Output: Sum of a and b

字符串

相关问题