笨方法学python第四版

大家好,小编来为大家解答以下问题,一个有趣的事情,一个有趣的事情,现在让我们一起来看看吧!

系统:mac os 10.14
Python: 2.7.10
版本:《笨办法学Python》(第四版)

基本习题

1. 完成基本习题

(1) 根据题目,编辑以下内容:

# -*- coding:utf-8 -*-
def cheese_and_crackers(cheese_count, boxes_of_crackers): # 定义函数cheese_and_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) # 调用cheese_and_crackers,并将数字作为参数传给它

print "OR, we can use variables from our :"
amount_of_cheese = 10 # 给变量赋值
amount_of_crackers = 50

cheese_and_crackers(amount_of_cheese, amount_of_crackers) #调用cheese_and_crackers,并将变量名传给它

print "We can even do math inside too:"
cheese_and_crackers(10 + 20, 5 + 6) #调用cheese_and_crackers,并将数字的运算结果传给它

print "And we can combine the two, variables and math:"
cheese_and_crackers(amount_of_cheese + 100, amount_of_crackers + 1000) #调用cheese_and_crackers,并将变量与数字的运算结果作为参数传给它

在这里插入图片描述

加分习题

1. 倒着将脚本读完,在每一行上面添加一行注解,说明这行的作用。

已备注在代码中

2. 从最后一行开始,倒着阅读每一行,读出所有的重要字符来火车头采集器AI伪原创

照做

3. 自己编至少一个函数出来,然后用 10 种方法运行这个函数。
# -*- coding:utf-8 -*-
from sys import argv

#定义函数cheese_and_crackers,该函数需接收两个输入参数
def cheese_and_crackers(cheese_count, boxes_of_crackers): 
	print "You have %d cheeses!" % cheese_count
	print "You have %d boxes of crackers!\n" % boxes_of_crackers

#case1:
print "We can just give the function numbers directly:"
cheese_and_crackers(20, 30) # 调用cheese_and_crackers,并将数字作为参数传给它

#case2:
print "OR, we can use variables from our :"
amount_of_cheese = 10 # 给变量赋值
amount_of_crackers = 50
cheese_and_crackers(amount_of_cheese, amount_of_crackers) #调用cheese_and_crackers,并将变量名传给它

#case3:
print "We can even do math inside too:"
cheese_and_crackers(10 + 20, 5 + 6) #调用cheese_and_crackers,并将数字的运算结果传给它

#case4:
print "And we can combine the two, variables and math:"
cheese_and_crackers(amount_of_cheese + 100, amount_of_crackers + 1000) #调用cheese_and_crackers,并将变量与数字的运算结果作为参数传给它

#case5:
print "case5"
cheese_and_crackers(amount_of_cheese, 100)

#case6:
print "case6"
cheese_and_crackers(int(raw_input("amount_of_cheese")), int(raw_input("amount_of_crackers")))

#case7:
print "case7"
cheese_and_crackers(int(raw_input("amount_of_cheese")) + 10, int(raw_input("amount_of_crackers")) + 10) 

print "case8"
cheese_and_crackers(int(raw_input("amount_of_cheese")) + amount_of_cheese, int(raw_input("amount_of_crackers")) + amount_of_crackers)

print "case9"
s, amount_of_cheese_again, amount_of_crackers_again = argv
cheese_and_crackers(int(amount_of_cheese_again), int(amount_of_crackers_again))

print "case10"
cheese_and_crackers(amount_of_cheese + int(amount_of_cheese_again), amount_of_crackers + int(amount_of_crackers_again))




在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值