因此,它看起来不错,但主要的问题是,你实际上并没有调用你的函数:)一旦你得到你的两个号码,可以然后拨打电话到你的函数(您已正确设置):
def main():
# When you assign variables here, make sure you are putting the int outside
# You also don't need to reference the variable twice
a = int(input("enter a number: "))
b = int(input("enter a number: "))
# Here is where your call goes (try to avoid using variable names that
# are the same as Python keywords, such as sum)
s = my_sum(a, b)
print(" result: ", s)
现在,你需要做的另一件事情就是修改你的函数返回一个值。你已经差不多了 - 只需添加一个回报(注意,因为你只是返回两个数字的总和,你不必把它分配给一个变量):
def my_sum(a, b):
return a + b
现在,这意味着,当您运行s = my_sum(a, b)
时,您的函数将返回这两个数字的总和并将它们放入s
,然后您可以按照您的要求打印。其他
一个次要的事情 - 当你使用你的设置(与def main()
,等等),你平时怎么称呼它是这样的:
if __name__ == '__main__':
main()
在这个阶段,不要太担心关于它是什么意思,但是一旦你开始进入模块等有趣的东西,进入它是一个好习惯。:)
回滚编辑。这是用Python 3标记的。原文是正确的。 –