习题 19: 函数和变量
在文本编辑器中,编辑以下内容并保存到ex19.py文件中,同时在终端中运行该文件:
##-- coding: utf-8 --
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 also 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)
执行结果:
$ 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 also 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 and math:
You have 110 cheeses!
You have 1050 boxes of crackers!
Man that's enough for a party!
Get a blanket.
加分习题
- 倒着将脚本读完,在每一行上面添加一行注解,说明这行的作用。
- 从最后一行开始,倒着阅读每一行,读出所有的重要字符来。
- 自己编至少一个函数出来,然后用 10 种方法运行这个函数。
def dogs_and_cats(amount_of_dogs, amount_of_cats):
print "I want to raise %d dogs." % amount_of_dogs
print "I also want to raise %d cats." % amount_of_cats
print "This is definitely fun.\n"
#第一种:
print "case one: "
dogs_and_cats(5, 6)
#第二种:
print "case two:"
dogs_num = 10
cats_num = 10
dogs_and_cats(dogs_num, cats_num)
#第三种:
print "case three:"
dogs_and_cats(10+10,2+4)
#第四种:
print "case four:"
dogs_and_cats(dogs_num+2,cats_num+3)
#第五种:
print "case five:"
dogs = int(raw_input("please input numbers of dog:"))
cats = int(raw_input("please input numbers of cat:"))
dogs_and_cats(dogs, cats)
#第六种:
print "case six:"
dogs_and_cats(dogs + dogs_num, cats + cats_num)
#第七种:
print "case seven:"
dogs_and_cats(dogs + 12, cats + 3)
#第八种:
print "case eight:"
dogs_and_cats(int(raw_input("dogs:")),int(raw_input("cats:")))
#第九种:
print "case nine:"
dogs_and_cats(int(raw_input("dogs:"))+dogs,int(raw_input("cats:"))+cats)
#第十种:
print "case nine:"
dogs_and_cats(int(raw_input("dogs:"))+3,int(raw_input("cats:"))+4)
执行结果:
$ python ex19.py
case one:
I want to raise 5 dogs.
I also want to raise 6 cats.
This is definitely fun.
case two:
I want to raise 10 dogs.
I also want to raise 10 cats.
This is definitely fun.
case three:
I want to raise 20 dogs.
I also want to raise 6 cats.
This is definitely fun.
case four:
I want to raise 12 dogs.
I also want to raise 13 cats.
This is definitely fun.
case five:
please input numbers of dogs:11
please input numbers of cat:22
I want to raise 11 dogs.
I also want to raise 22 cats.
This is definitely fun.
case six:
I want to raise 21 dogs.
I also want to raise 32 cats.
This is definitely fun.
case seven:
I want to raise 23 dogs.
I also want to raise 25 cats.
This is definitely fun.
case eight:
please input numbers of dogs:33
please input numbers of cats:44
I want to raise 33 dogs.
I also want to raise 44 cats.
This is definitely fun.
case nine:
please input numbers of dogs:55
please input numbers of cats:66
I want to raise 66 dogs.
I also want to raise 88 cats.
This is definitely fun.
case nine:
please input numbers of dogs:77
please input numbers of cats:88
I want to raise 80 dogs.
I also want to raise 92 cats.
This is definitely fun.