大家好,小编来为大家解答以下问题,一个有趣的事情,一个有趣的事情,现在让我们一起来看看吧!
系统: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))