ex19 变量和函数

通过这个练习,你看到我们给我们的函数 cheese_and_crackers 很多的参数,
然后在函数里把它们打印出来。我们可以在函数里用变量名,我们可以在函数里
做运算,我们甚至可以将变量和运算结合起来。

def cheese_and_crackers(cheese_count,boxes_of_crackers):
    print("You have %d cheese." %cheese_count)
    print("And you also have %d boxes of crackers." %boxes_of_crackers)
    print("Man that's enough for a party!")
    print("Get a blanket.\n")

print("可以直接给两个参数进行赋值:")
cheese_and_crackers(10,20)

print("We can use variables for the scripts:")
amount_of_chess = 30
amount_of_crackers = 40
cheese_and_crackers(amount_of_chess,amount_of_crackers)

print("We can even do math inside too:")
cheese_and_crackers(1+2,3+4)

print("And we can combine the two,variables and math")
cheese_and_crackers(amount_of_chess+3,amount_of_crackers+5)

运行结果

可以直接给两个参数进行赋值:
You have 10 cheese.
And you also have 20 boxes of crackers.
Man that’s enough for a party!
Get a blanket.

We can use variables for the scripts:
You have 30 cheese.
And you also have 40 boxes of crackers.
Man that’s enough for a party!
Get a blanket.

We can use variables for the scripts:
You have 30 cheese.
And you also have 40 boxes of crackers.
Man that’s enough for a party!
Get a blanket.

We can even do math inside too:
You have 3 cheese.
And you also have 7 boxes of crackers.
Man that’s enough for a party!
Get a blanket.

And we can combine the two,variables and math
You have 33 cheese.
And you also have 45 boxes of crackers.
Man that’s enough for a party!
Get a blanket.

总结

在使用函数的时候,可以对里面的变量进行数学运算,也可以定义新的变量,用新变量来表示函数的参数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值