习题19
python:3.9
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 fuction 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)
运行结果
PS C:\Users\78523\mybuff> python ex19.py
We can just give the fuction 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.
加分习题
- 倒着将脚本读完,在每一行上面添加一行注解,说明这行的作用。
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 fuction 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)
-
从最后一行开始,倒着阅读每一行,读出所有的重要字符来。
-
自己编至少一个函数出来,然后用10种方法运行这个函数。
def Coca(bottles_of_Coca):
print ("you have %d bottles of Coca."%bottles_of_Coca)
print ("So,you can have a good drink.")
Coca (66)
amount_of_Coca=22
Coca(amount_of_Coca)
Coca (30+30)
Coca(amount_of_Coca+44)
simple=int(input("?"))
Coca(simple)
Coca(simple+10)
Coca(int(input("?")))
Coca(int(input("?"))+20)
from sys import argv
script,Coca=argv
print("We have %r bottles of Coca."%Coca)
print("We have %r bottles of Coca."%(Coca*10))
运行结果
PS C:\Users\78523\mybuff> python ex19.19.py 66
you have 66 bottles of Coca.
So,you can have a good drink.
you have 22 bottles of Coca.
So,you can have a good drink.
you have 60 bottles of Coca.
So,you can have a good drink.
you have 66 bottles of Coca.
So,you can have a good drink.
?66
you have 66 bottles of Coca.
So,you can have a good drink.
you have 76 bottles of Coca.
So,you can have a good drink.
?66
you have 66 bottles of Coca.
So,you can have a good drink.
?66
you have 86 bottles of Coca.
So,you can have a good drink.
We have '66' bottles of Coca.
We have '66666666666666666666' bottles of Coca.
这里我想说抄别人的编码真的是简单,但是自己想真的要想好久,并且还不知道自己写的对不对,真的很难受。
最后我放上另一个博主的链接,他写是真不错,大家可以借鉴借鉴,同时感谢博主的无私奉献!
学习笔记