因此,它看起來不錯,但主要的問題是,你實際上並沒有調用你的函數:)一旦你得到你的兩個號碼,可以然後撥打電話到你的函數(您已正確設置):
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標記的。原文是正確的。 –