您的位置:首页 > 编程语言 > Python开发

笨方法学习Python 习题19 函数和变量 ---学习记录

2017-12-14 09:28 645 查看

习题19 函数和变量

#定义cheese_and_crackers函数,参数有括号里的俩

def cheese_and_crackers(cheese_count,boxes_of_crackers):

print “1.You have %d cheeses” % cheese_count

print “2.You have %d boxes of crackers!” % boxes_of_crackers

print “3.Man that’s enough for a party!\n4.Get a blanket.”

开始第一次调用,使用函数cheese_and_crackers的20 30值打印 5 1234

print “5.We can just give the function numbers directly:”

cheese_and_crackers(20,30)

开始第二次调用,使用函数cheese_and_crackers的10 50值打印 6 1234

print “6.OR,we can use variable from our script:”

amount_of_cheese = 10

amount_of_crackers = 50

cheese_and_crackers(amount_of_cheese,amount_of_crackers)

开始第三次调用,使用函数cheese_and_crackers的30 11值打印 7 1234

print “7.We can even do math inside too:”

cheese_and_crackers(10+20,5+6)

开始第四次调用,使用函数cheese_and_crackers

print “8.And we can combine the two,variable and math:”

cheese_and_crackers(amount_of_cheese+100,amount_of_crackers+1000)

def cheese_and_crackers(cheese_count,boxes_of_crackers):

print “1.You have %s cheeses” % cheese_count

print “2.You have %s boxes of crackers!” % boxes_of_crackers

print “3.Man that’s enough for a party!\n4.Get a blanket.”

开始第一次调用,使用函数cheese_and_crackers的20 30值打印 5 1234

print “5.We can just give the function numbers directly:”

cheese_and_crackers(20,30)

开始第二次调用,使用函数cheese_and_crackers的10 50值打印 6 1234

print “6.OR,we can use variable from our script:”

amount_of_cheese = 10

amount_of_crackers = 50

cheese_and_crackers(amount_of_cheese,amount_of_crackers)

开始第三次调用,使用函数cheese_and_crackers的30 11值打印 7 1234

print “7.We can even do math inside too:”

cheese_and_crackers(10+20,5+6)

开始第四次调用,使用函数cheese_and_crackers

print “8.And we can combine the two,variable and math:”

cheese_and_crackers(amount_of_cheese+100,amount_of_crackers+1000)

开始第五次调用,使用raw_input

print “9. please input your favorite number:”

number1 = raw_input(‘>’)

number2 = raw_input(‘>’)

cheese_and_crackers(number1,number2)

开始第六次调用,使用write

print “9. we will use write method to list those:”

from sys import argv

script,filename = argv

test = open(filename,’w’)

line1 = raw_input(‘>’)

line2 = raw_input(‘>’)

test.write(line1+’\n’+line2+’\n’)

cheese_and_crackers(line1,line2)

test.close()

开始第七次调用 使用 read—-写不出来啦

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息