3/26 星期二

昨天加班太晚,没有时间看书了,今天抽空做了ex19的练习

def cheese_and_crackers(cheese_count,boxes_of_crackers):

      print(f"You have {cheese_count} cheeses!")

      print(f"You have {boxes_of_crackers} cheeses!")

      print("Man that's enough for a party!")

      print("Get a blanket.")

      print("你好坏\n")

print("你好毒")

print("We can just give the function numbers directly:")#1

cheese_and_crackers(20,30)#2

print("Or,we can use varibales from our script:")#1

amount_of_cheese=10

amount_of_crackers=50

cheese_and_crackers(amount_of_cheese,amount_of_crackers)#2

print("We can even do math inside too:")#1

cheese_and_crackers(10+20,5+6)#2

print("And we can combine the two,varibales and math:")#1

cheese_and_crackers(amount_of_cheese+100,amount_of_crackers+1000)#2

#定义cheese_and_crackers有两个变量,执行顺序是先执行“1”的命令,执行到"2"的时候返回到def下面的命令内容

从零开始学人工智能--笨办法学python(五)

发现输出结果有重复的地方,仔细分析了一下,我是这样理解的:

def定义了函数,后面每个print命令下面都针对函数里的内容进行了变量定义,所以每次执行命令时,先优先执行独立print的命令,也就是标注了"1"的部分;执行完print后,再执行变量,也就是标注了“2”的部分,而“2”的部分是属于def命令中的部分,所以才循环执行def里面的print。

 

相关文章: