Python def sum(num1, num2) 语法错误

Python syntax error on def sum(num1, num2)

我在初学者编程 class 我应该让这段代码正常工作,我一遍又一遍地按照书上说的输入它,但我得到了一个无效的语法错误,在 Python Idle 中突出显示 def in def sum(num1, num2) 我到处看了看,在书中,在网上,但还没有找到为什么它给我一个错误的答案。

#this program uses the return value of a function

def main():
    #Get the user's age
    first_age = int(input('Enter your age: '))

    #Get the user's best friend's age.
    second_age = int(input("Enter your best friend's age: "))

    #Get the sum of both ages
    total = sum(first_age, second_age)

    #Display the total age
    print('Together you are', total, 'years old.')

#the sum function accepts two numeric arguments and
# return the sum of those arguments.
def sum(num1, num2):
    result = num1 + num2
    return result

#Call the main function

main()

SyntaxError: invalid syntax

您的代码运行。你有像下面这样的确切缩进吗?我必须修复其中的一些才能使其正常工作。

def main():
    #Get the user's age
    first_age = int(input('Enter your age: '))

    #Get the user's best friend's age.
    second_age = int(input("Enter your best friend's age: "))

    #Get the sum of both ages
    total = sum(first_age, second_age)

    #Display the total age
    print('Together you are', total, 'years old.')

#the sum function accepts two numeric arguments and
# return the sum of those arguments.
def sum(num1, num2):
    result = num1 + num2
    return result

#Call the main function

main()