iT邦幫忙

第 12 屆 iThome 鐵人賽

0
自我挑戰組

笨方法學python 大家來一起開心學python XD系列 第 7

Day7:函數返回

我們已經使用過"="給變量命名,今天主要演示的是如何使用"return"來將變量設置成一個函數的值。

[In]
def subtract(a,b):
    print=(f"subtracting{a} - {b}")
    return a-b
def add(a,b):
    print (f"adding{a}+{b}")
    return a+b

def multily(a,b):
    print(f"multilying {a}*{b}")
    return a*b
def divide(a,b):
    print(f"dividing{a}/{b}")
    return a/b

print('let`s do some math')

candy=subtract(20,5)
soda=add(1,2)
man=multily(10,20)
woman=divide(30,15)
print("There have {} candy {} soda {} man {} woman ".format(candy,soda,man,woman))

###注意format如果要這樣用會像這樣e.g. print("There heva st{}".format(bug)) 
#注意後面的brackets
[Out]
let`s do some math
adding1+2
multilying 10*20
dividing30/15
There have 15 candy 3 soda 200 man 2.0 woman 

今日總結:

確認是否了解return的意思?


上一篇
Day6:class函數
下一篇
Day8:如果怎樣就怎樣
系列文
笨方法學python 大家來一起開心學python XD12
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言