笨办法学Python3 习题19 函数和变量

发布时间 2023-10-04 19:37:21作者: 萹豆

定义调用函数:

定义 函数( 参数变量1,参数变量2):

        函数命令 打印 你有参数变量1的干酪

        打印你有 参数变量2的 干酪盒子               

        打印句子        

打印句子1 

调用函数(参数值1,参数值2)

 

打印句子2

赋值变量1,变量2

调用函数 (变量1,变量2)

 

打印句子3

调用函数 (10+20,5+6)

 

打印句子4

调用函数(变量1+100,变量2+1000)

 

 1 def cheese_and_crackers(cheese_count, boxes_of_crackers):  # 定义函数名,参数1,参数2
 2     print(f"You have {cheese_count} cheeses!")                   # 函数命令打印..参数1...
 3     print(f"You have {boxes_of_crackers} boxes of crackers!")    # 函数命令打印..参数2...
 4     print("Man that's enough for a party!")                      # 函数命令打印......
 5     print("Get a blanket.")                                      # 函数命令打印......
 6 
 7 print("We can just give the function numbers directly:")   #打印 字符串
 8 cheese_and_crackers(20,30)                                 #调用函数,给参数1,参数2 临时 赋值
 9 
10 print("OR, we can use variables from our script:")           # 打印 字符串
11 amount_of_cheese = 10                                        # 赋值给变量1
12 amount_of_crackers =50                                       # 赋值给变量2
13 cheese_and_crackers(amount_of_cheese, amount_of_crackers)    # 调用函数,将参数值 临时 改为变量1,变量2
14 
15 print("We can even do math inside too:")                   # 打印字符串
16 cheese_and_crackers(10+20,5+6)                             # 调用函数,将计算值 临时 赋给参数
17 
18 print("And we can combine the two, variables an math:")             # 打印字符串
19 cheese_and_crackers(amount_of_cheese+100, amount_of_crackers+1000)  # 调用函数,将参数值 临时 改为变量1+100,变量2+1000
PS C:\Users\Administrator\lpthw> python ex19.py
We can just give the function numbers directly:
You have 20 cheeses!
You have 30 boxes of crackers!
Man that's enough for a party!
Get a blanket.
OR, we can use variables from our script:
You have 10 cheeses!
You have 50 boxes of crackers!
Man that's enough for a party!
Get a blanket.
We can even do math inside too:
You have 30 cheeses!
You have 11 boxes of crackers!
Man that's enough for a party!
Get a blanket.
And we can combine the two, variables an math:
You have 110 cheeses!
You have 1050 boxes of crackers!
Man that's enough for a party!
Get a blanket.