《笨方法学 Python 3》19.函数和变量

基础练习:

本节课主要是讲在调用函数传参时可是使用多种方式传参,Zed说理论上有无穷多中方式,本课主要讲了常用的几种方式。

#def定义一个变量,cheese奶酪, crackers饼干
def cheese_and_crackers(cheese_count, boxes_of_crackers):
	print(f"You have {cheese_count} cheeses!")
	print(f"You have {boxes_of_crackers} boxes of crackers!")
	print("Man that's enough for a party!")
	print("Get a blanket.\n")

print("我们可以直接给函数传入数字")
#调用函数
cheese_and_crackers(20,30)

print("或者,我们可以使用脚本中的变量:")
#下面的两个变量是全局变量
amount_of_cheese = 10
amount_of_crackers = 50

#调用函数
cheese_and_crackers(amount_of_cheese, amount_of_crackers)

print("我们甚至可以在里面做数学运算:")
#调用函数
cheese_and_crackers(10+20,5+6)

print("我们可以把变量和数学运算起来")
#调用函数
cheese_and_crackers(amount_of_cheese + 100,amount_of_crackers + 10000)

结果:

没啥干货了,END!!! 

猜你喜欢

转载自blog.csdn.net/waitan2018/article/details/82533539