《笨办法学python》加分习题19——我的答案

这是我自己学习的答案,会尽力写的比较好。还望大家能够提出我的不足和错误,谢谢!

文中例题:

def cheese_and_crackers(cheese_count, boxes_of_crackers):
    print "You have %d cheeses!" % cheese_count
    print "You have %d 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 even 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)

习题答案

1、

# -- coding: utf-8 --

# 声明了cheese_and_crackers这个函数,该函数有两个参数,分别是cheese_count和boxes_of_crackers.
# 函数其实就是个print的集合
def cheese_and_crackers(cheese_count, boxes_of_crackers):
    print "You have %d cheeses!" % cheese_count
    print "You have %d 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:"
# 调用了这个函数,带入两个参数(20, 30)
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 even 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

3、姑且原谅我只想到文中四种吧,突然让我想,我好像没什么东西啊。。。果然基础还是太薄弱。望大家给点想法。

# -- coding: utf-8 --
def My_first_function(first, second):
    print "this is first number: %d" % first
    print "this is second number: %d" % second

My_first_function(1, 2)

My_first_function(1 + 1, 2 + 2)

first_num = 1;
second_num = 2;
My_first_function(first_num, second_num)

My_first_function(first_num + 1, second_num + 2)

# 想不到了
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值