其他分享
首页 > 其他分享> > 习题35:分支和函数

习题35:分支和函数

作者:互联网

1. 代码

from sys import exit
# 定义黄金屋函数
def gold_room():
    print("This room is full of gold. How much do you take?")
    next = input(">")
    if "0" in next or "1" in next:
        how_much = int(next)
    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 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

# 定义死亡函数
def dead(why):
    print(why,'Good job!')
    exit(0)

# 定义克苏鲁房间
def cthulhu_room():
    print("Here you see the great evil Cthulhu.")
    print("He, it, whatever starts at you and you go insane.")
    print("Do you flee for your life or eat your head?")
    next = input(">")
    if "flee" in next:
        start()
    elif "head" in next:
        dead("Well that was tasty!")
    else:
        cthulhu_room()
# 定义开始函数
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?")

    next = input("> ")
    if next == "left":
        bear_room()
    elif next == "right":
        cthulhu_room()
    else:
        dead("You stumble around the room until you starve.")
# 执行开始函数
start()

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

  2. 执行结果

D:\流畅学python\venv\Scripts\python.exe D:/流畅学python/ex35.py
You are in a dark room.
There is a door to your right and left.
Which one do you take?
> left
There is a bear here.
The bear has a bunch of honey.
The fat bear is in front of another door.
How are you going to move the bear?
>taunt bear
The bear has moved from the door. You can go through it now.
>open door
This room is full of gold. How much do you take?
>asf
Man, learn to type a number. Good job!

Process finished with exit code 0

  3. 流程图

 

标签:door,函数,bear,next,35,print,习题,dead,room
来源: https://www.cnblogs.com/kevin-hou1991/p/14773899.html