《笨办法学python3-Learn Python 3 the HARD WAY》-习题19 函数和变量

学习内容:

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)
  1. 自己编写一个函数,并用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)

运行结果:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值