《笨办法学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)

# 想不到了

你可能感兴趣的:(《笨办法学python》加分习题19——我的答案)