学习内容:
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 ("We can just give the function numbers directly:")
cheese_and_crackers(20, 30)
print ("OR, we can use variables from our script:")
amount_of_cheese = 10
amount_of_crackers = 50
cheese_and_crackers(amount_of_cheese, amount_of_crackers)
print ("We can eben do math inside too:")
cheese_and_crackers(10 + 20, 5 + 6)
print ("And we can combine the two, variables and math:")
cheese_and_crackers(amount_of_cheese + 100, amount_of_crackers + 1000)
运行结果:
知识点:
给每一行用"#"加注释
# 定义函数 cheese_and_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")
# 方法一:调用函数 cheese_and_crackers并给函数赋予两个参数
print ("We can just give the function numbers directly:")
cheese_and_crackers(20, 30)
# 方法二:调用函数 cheese_and_crackers并给函数赋予两个参数。先定义变量,在将变量定为函数中的两个参数
print ("OR, we can use variables from our script:")
amount_of_cheese = 10
amount_of_crackers = 50
cheese_and_crackers(amount_of_cheese, amount_of_crackers)
# 方法三:调用函数 cheese_and_crackers并给函数赋予两个参数。参数用运算方法定义
print ("We can eben do math inside too:")
cheese_and_crackers(10 + 20, 5 + 6)
# 方法四:调用函数 cheese_and_crackers并给函数赋予两个参数。参数用变量和运算方法定义
print ("And we can combine the two, variables and math:")
cheese_and_crackers(amount_of_cheese + 100, amount_of_crackers + 1000)
- 自己编写一个函数,并用10种不同方式运行这个函数。
只想出5种:
def cookies_and_children(cookise, children):
print (f"We have {cookise} cookies.")
print (f"We have {children} children.")
print ("I think that's enough!")
print ("Calculation method 1:")
cookies_and_children(66, 23)
print ("Calculation method 2:")
cookise_count = input("cookisenum:")
children_count = input("childrennum:")
cookies_and_children(cookise_count, children_count)
print ("Calculation method 3:")
cookies_and_children(22 + 12, 17 + 2)
print ("Calculation method 4:")
Chocolate_biscuit = 33
Milk_biscuits = 12
cookies_and_children(Chocolate_biscuit + Milk_biscuits, 14)
print ("Calculation method 5:")
children_girl = 20
children_boy = 15
cookies_and_children(Chocolate_biscuit + Milk_biscuits + 50, children_girl + children_boy + 30)
运行结果: