“笨办法”学Python3,Zed A. Shaw,习题19

习题19

  • 代码
  • 遇到问题
  • 学到的

代码

def cheese_and_crackers(cheese_count, boxes_of_crackers):
    print(f"You have {cheese_count} cheeses!")
    print(f"You have {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)#answer will be 20 and 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)#answer will be 10 and 50.

print("We can even do math inside too:")
cheese_and_crackers(10+20, 5+6)#answer will be 30 and 11.

print("And we can combine the two, variables and math:")
cheese_and_crackers(amount_of_cheese + 100, amount_of_crackers + 1000)#answer will be 110 and 1050.
from sys import argv
script, who, when, where, filename1, filename2, filename3 = argv
def Blue0():#不知道括号内要不要写成({who}, {when}, {where})
    print(f"{when} , {who} let you feel terrible and blue. {where} is not a save place anymore.")

def Blue1():
    who = input("who let you down? >>>>>")
    when = input("when does the blue happen? >>>>>")
    where = input("where does this happen? >>>>>")
    print(f"{when} , {who} let you feel terrible and blue. {where} is not a save place anymore.")

def Blue2(who, when, where):
    print(f"{when} , {who} let you feel terrible and blue. {where} is not a save place anymore.")

def Blue3(who, when, where):
    print(f"{when} , {who} let you feel terrible and blue. {where} is not a save place anymore.")

def Blue4(*wh):#wh是一个列表
    who, when, where = wh#把who, when, where3个元素加到wh这个列表里
    print(f"{when} , {who} let you feel terrible and blue. {where} is not a save place anymore.")

def Blue5():
    who = open("ex19_ex01.txt")#open要和print一样用,加“”,文件才会打开。而用argv函数,在启动脚本时输入文件名,就不需要“”了。
    when = open("ex19_ex02.txt")
    where = open("ex19_ex03.txt")
    print(f"{when.read()} , {who.read()} let you feel terrible and blue. {where.read()} is not a save place anymore.")

def Blue6():
    who = open(filename1).read()
    when = open(filename2).read()
    where = open(filename3).read()
    print(f"{when} , {who} let you feel terrible and blue. {where} is not a save place anymore.")

def file(filename):
    open(filename).read()
def Blue7():
    #def file(filename):
    #    open(filename).read()
    #即便把file()函数套到Blue7()里也没有用,应该python里没这种用法,哈哈。
    who = file(filename1)
    when = file(filename2)
    where = file(filename3)#file()函数似乎没法把值传到这里?还是格式错了?
    print(who)
    print(f"{when} , {who} let you feel terrible and blue. {where} is not a save place anymore.")


Blue0()
Blue1()
Blue2("alex", "tomorrow", "here")
who = 11
when = 12
where = 13
Blue3(who, when, where)#因为下面的Blue3要用到3个参数,所以原函数里不能删掉3个参数,因此这里运行也就不能删掉3个参数啦。
Blue3(who + 10, when + 10, where +10)
Blue4(31, 32, 33)
Blue5()
Blue6()
Blue7()#想通过一个函数file读出文件的内容,然后用blue7函数打印出结果,失败了,不知道什么原因,先放着吧。

遇到问题

1、主要就是部分函数里的最后一个函数,没法实现。第二部分写了9种方法实现一个相同的情况。第9个方法失败了,不知为什么。

学到的

1、函数括号内的变量参数可以用另外的变量、数值、算式带入;
2、open的用法和print差不多,open直接带上文件名时,要加引号“”;
3、有时候后面括号内的参数可以省略。

你可能感兴趣的:(python)