【lphtw】第三弹笔记ex24-ex35

练习24 夯实基础

练习24中并无其他特殊部分,需要关心的有

        1.转义符的运用(\ \t \n  ''' ''' )

        2.赋值后F格式的print,花括号中放变量名

           pirnt中带花括号{}中间不放变量名,.format(变量)去输出

        3.函数的定义以及唤起

def secret_formula(started):
    jelly_beans = started * 500
    jars = jelly_beans / 1000
    crates = jars / 100
    return jelly_beans, jars, crates


start_point = 10000
beans, jars, crates = secret_formula(start_point)

        这三个变量是有关于 私密方程 这个函数的结果,被return赋值

        以及打包变量,注意这个地方的*formula,他包含了三个参数,分别对应按顺序的赋值

print("We can also do that this way:")
formula =secret_formula(start_point)
#this is an easy way to apply a list to a format string
print("We'd have {} beans, {} jars, and  {} crates.".format(*formula))

——————————————————————————————————————————

练习25 接触几个新函数

        本例中没有直接调用函数,所以像之前一样python ex**.py直接调用是不会有任何输出的

        选择先进入py环境,然后再使用import导入ex25

        函数:split() Python中有split()和os.path.split()两个函数,具体作用如下:

        split(): 通过指定分隔符对字符串进行切片,并返回分割后的字符串列表(list)

        sorted() 函数用于对原列表进行排序,如字母表顺序

        pop()函数,输出列表中第 顺序的元素,正序从0开始,倒序从-1开始

———————————————————————————————————————————

练习29,30 if语句 else语句

        if语句,记得加冒号,下方代码缩进4个空格,同一个层次的if要对齐,py不用end if 但是要严格对齐。如果if内容为true,则执行其下方内容

        py中branch的选择用if,elif,else,其中if和elif作用相同,可有多个elif

        如果多个 elif 块都是 True 会发生什么? Python 从顶部开始,然后运行第一个是 True 的代码块,也就是说,它只会运行第一个。

——————————————————————————————————————————

练习31 嵌套if

        本例中注意同一个层次if要对齐,同属于一个if的函数代码等例如print也要对齐,在编辑器可以打出来,通过验证 input的输入=if 中的参数  来选择对应执行的代码。最后一个if的选项执行使用else

———————————————————————————————————————————

练习32 for_____in_____循环

        1.了解列表是怎样的一个东西,方括号

        2.for  in循环

        当for  中的东西满足在循环中指型,则会执行至不满足循环,而这变量其实不需要满足其他这个脚本中的需求,甚至可以没有出现过,但是他要与for循环中的的{}中的对应

        3.append()函数,将()中的元素添加到list中

———————————————————————————————————————————

练习33 while 循环

        只要一个布尔表达式是 True,while-loop 就会一直执行它下面的代码块。直到检测条件为false。

———————————————————————————————————————————

练习35 练习一个游戏

        首先要import一个exit函数,exit(1) 表示发生了错误进行退出,而 exit(0) 则表示程序是正常退出的,退出代码是告诉解释器的(或操作系统)。

        定义几个房间,分别是金子房和熊房、克鲁苏房间

        通过调用start函数,对choice进行赋值,进行选择,if left ;elif right分别对应熊房子和克鲁苏房子,如果输入其他的直接调用dead函数,dead是关于(why)的函数,why的内容则写在else中

def dead(why):
    print(why, "Good job!")
    exit(0)

def start():
    print("You are in a dark room")
    print("There is a door to your right and left.")
    print("Which one do you take?")

    choice = input("> ")

    if choice =="left":
        bear_room()
    elif choice == "right":
        cthulhu_room()
    else:
        dead("You stumble around the room until you starve")

start()

        即如果没有选择就会被饿死。如果选择了熊房,则调用熊房函数,有baer_moved这个变量在,要使bear_move 这个值变成true后,再回到循环中,选择open door,调用gold_room函数走出熊房

def bear_room():
    print("There is a bear here.")
    print("The bear has a bunch of honey.")
    print("The fat bear is in front of another door.")
    print("How are you going to move the bear?")
    bear_moved = False

    while True:
        choice = input("> ")

        if choice =="take honey":
            dead("The bear looks at you then slaps your face")
        elif choice == "taunt bear" and not bear_moved:
            print("The bear has moved from the door.")
            print("You can go through it now")
            bear_moved = True
        elif choice =="tauat bear" and bear_moved:
            dear("The bear gets pissed off and chews your leg")
        elif choice =="open door" and bear_moved:
            gold_room()
        else:
            print("I got no idea what that means.")

        进入金房后,设置要拿走的金块的数量,必须包含0或者1字符(计算机开的玩笑),如果没有,则执行第一个else,接下来就是第二个分支,对how_much进行判断

def gold_room():
    print("This room is full of gold. How much do you take?")

    choice = input("> ")

    if "0" in choice or "1" in choice:
        how_much = int(choice)
    else:
        dead("Man, learn to type a number.")

    if how_much < 50:
        print("Nice, you're not greedy, you win!")
        exit(0)
    else:
        dead("You greedy bastard!")

        克鲁苏房间就相对简单了,但是值得注意的是,选择分支中他重新调用这个函数,使得自动开始循环回到开头

def cthulhu_room():
    print("Here you see the great evil Cthulhu.")
    print("He, it, whatever stares at you and you go insane.")
    print("Do you flee for your life or eat your head?")

    choice = input("> ")

    if  "flee" in choice:
        start()
    elif "head" in choice:
        dead("Well that was tasty!")
    else:
        cthulhu_room()

———————————————————————————————————————————

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值