笨方法学python 习题19

习题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.

加分习题

  1. 倒着将脚本读完,在每一行上面添加一行注解,说明这行的作用。
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)
  1. 从最后一行开始,倒着阅读每一行,读出所有的重要字符来。

  2. 自己编至少一个函数出来,然后用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.

这里我想说抄别人的编码真的是简单,但是自己想真的要想好久,并且还不知道自己写的对不对,真的很难受。
最后我放上另一个博主的链接,他写是真不错,大家可以借鉴借鉴,同时感谢博主的无私奉献!
学习笔记

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值