天天看点

《笨办法学python3-Learn Python 3 the HARD WAY》-习题35 分支和函数

学习内容:

from sys import exit

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, you're not greedy, you win!")

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


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? take honey or taunt bear")
    bear_moved = False

    while True:
        choice = input("> ")

        if choice == "take honey":
            dead("The bear looks at you then slaps your face off.")
        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 == "taunt bear" and bear_moved: 
            dead ("The bear gets pissed off and chews your leg off.")
        elif choice == "open door" and bear_moved:
            gold_room()
        else:
            print ("I got no idea what that means.")


def cthulhu_room():
    print ("Here you see the great evil Cthulhu.")
    print ("He, it, whatever stares st 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("Wekk that was tasty!")
    else:
        cthulhu_room()


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()
           

运行结果:

游戏失败:

《笨办法学python3-Learn Python 3 the HARD WAY》-习题35 分支和函数

游戏成功:

《笨办法学python3-Learn Python 3 the HARD WAY》-习题35 分支和函数

知识点:

  1. 我不能理解的部分加上"#"注释
from sys import exit

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, you're not greedy, you win!")

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


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? take honey or taunt bear")
    bear_moved = False

    while True:
        choice = input("> ")

        if choice == "take honey":
            dead("The bear looks at you then slaps your face off.")
        elif choice == "taunt bear" and not bear_moved: # 这里的bear_moved为False(24行定义bear_moved函数为False)
            print ("The bear has moved from the door.")
            print ("You can go through it now.")
            bear_moved = True
        elif choice == "taunt bear" and bear_moved: # 这里的bear_moved为True(34行定义bear_moved函数为True)
            dead ("The bear gets pissed off and chews your leg off.")
        elif choice == "open door" and bear_moved:
            gold_room()
        else:
            print ("I got no idea what that means.")


def cthulhu_room():
    print ("Here you see the great evil Cthulhu.")
    print ("He, it, whatever stares st 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("Wekk that was tasty!")
    else:
        cthulhu_room()


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()
           
  1. 游戏的地图
    《笨办法学python3-Learn Python 3 the HARD WAY》-习题35 分支和函数
  2. gold_room

    中奇怪的方式

    判断输入的是否为整数

    if "0" in choice or "1" in choice:# 判断输入的是否为数字

    可以使用

    choice = int(input("> "))

  3. exit(0)

    exit(0)

    可以终止某个程序,表示正常退出。

    exit(1)

    表示发生了错误
from sys import exit

exit(0)
           

5.

while True

while Ture

无线循环的常用方法,判断表达式的值本身就是True,while循环将无线进行下去。

6.

isdigit()

if "0" in choice or "1" in choice:

用来判断输入的是否为数字。可以用

if choice.isdigit()

isdigit()

监测字符串是否为数字组成

语法:

str.isdigit()

若是由数字组成则返回True,否则为False。

7. in 与 is的区别

in 和 not in 用于容器类型的成员判断;例如 if x in y,如果x是s的成员,将返回True,反之为False

is 和 not is 用于对象身份的判断,只有x和y同一个对象时,x is y返回True,反之为False

继续阅读