习题35:分支和函数

1. 代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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. 执行结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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. 流程图

 

posted @   JRS077  阅读(44)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示