赞
踩
- # 从sys库中导入exit函数
- from sys import exit
-
-
- # 定义gold_room()函数
- def gold_room():
- print("这个房间里全是金子你要拿走多少?")
-
- # 定义局部变量choice
- choice = input(">")
- # 创建if块,若choice中含有0 / 1,运行下一行代码
- if "0" in choice or "1" in choice:
- # 定义局部变量how_much
- how_much = int(choice)
- # 若choice中无0 / 1,运行如下代码
- else:
- dead("老兄学着写入数字吧!.")
-
- # 创建if块,若how_much > 50,运行下一行代码
- if how_much < 50:
- print("很好,你不贪心,你赢了啊!")
- exit(0)
- # how_much <= 50,则运行以下代码
- else:
- dead("你这个贪婪的人!")
-
-
- # 定义bear_room()函数
- def bear_room():
- # 打印字符串
- print("这里有一只熊.")
- print("熊有一串蜂蜜.")
- print("胖熊在另一扇门前.")
- print("你打算怎么赶走这只熊呢?")
- # 定义局部变量bear_moved为布尔表达式
- bear_moved = False
-
- # 创建while循环
- while True:
- # 创建局部变量choice
- choice = input(">")
-
- # 创建if块,若choice为take honey,则运行语句
- if choice == "拿走蜂蜜":
- dead("熊看着你,抓了你的脸.")
- # 若choice为“taunt bear”且bear_moved为假,运行语句
- elif choice == "嘲笑熊" and not bear_moved:
- print("熊已经走了.")
- print("你现在可以看到.")
- bear_moved = True
- # 若choice为“taunt bear”且bear_moved为真,运行语句
- elif choice == "taunt bear" and bear_moved:
- # 读取dead()函数
- dead("熊生气了,咬了你的腿.")
- # 若choice为“open door”且bear_moved为真,运行语句
- elif choice == "open door" and bear_moved:
- gold_room()
- # 其他情况运行以下代码
- else:
- print("我不知是什么意思.")
-
-
- # 定义cthulhu_room()函数
- def cthulhu_room():
- print("你看到的邪恶.")
- print("它盯着你,你就要疯掉了.")
- print("你是被吃了头还是逃跑了?")
-
- choice = input(">")
-
- # 若choice中有“flee”,运行语句
- if "flee" in choice:
- start()
- # choice中有“head”,运行语句
- elif "head" in choice:
- dead("太好吃了!")
- # 其余情况,运行语句
- else:
- cthulhu_room()
-
-
- # 定义dead(why)函数
- def dead(why):
- print(why, "干的漂亮!")
- # 退出程序
- exit(0)
-
-
- # 定义start()函数
- def start():
- print("你在一个黑暗的房子里.")
- print("这个门你向右和左边走呢?.")
- print("你选择那边?")
-
- choice = input(">")
-
- # 若choice为“left”,运行语句
- if choice == "left":
- bear_room()
- # 若choice为“right”,运行语句
- elif choice == "right":
- cthulhu_room()
- # 若为其他情况,运行语句
- else:
- dead("你在房间里四处乱跑,直到饿死了.")
-
- # 运行start()函数
- start()
- D:\软件\py36\python.exe E:\python\day15\31\xiti35.py
- 你在一个黑暗的房子里.
- 这个门你向右和左边走呢?.
- 你选择那边?
- >left
- 这里有一只熊.
- 熊有一串蜂蜜.
- 胖熊在另一扇门前.
- 你打算怎么赶走这只熊呢?
- >open door
- 我不知是什么意思.
- >嘲笑熊
- 熊已经走了.
- 你现在可以看到.
- >open door
- 这个房间里全是金子你要拿走多少?
- >80
- 你这个贪婪的人! 干的漂亮!
-
- 进程已结束,退出代码0
巩固练习
把这个游戏的地图画出来,把自己的路线也画出来
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-pR4lsFoD-1592122475333)(D:\google下载\You are in a dark room…png)]
改正你的所有错误,包括拼写错误
为你不懂的函数写注释
为游戏添加更多元素。通过怎样的方式可以简化并且扩展游戏的功能?
这个gold_room游戏使用了奇怪的方式让你键入一个数。这种方式会导致什么样的bug?你能让它比我写的程序更好吗?int()这个函数可以给你一些头绪。
不含0/1的数字被误认,导致游戏死亡结束
判断choice的值是否为整数
一个整数除以自身 = 1
一个整数减去自身 = 0
if int(choice)/int(choice) == 1
1
exit()函数
在很多类型的操作系统里,exit() 可以中断某个程序,而其中的数字参数则用来表示程序是否是碰到错误而中断。
exit(0) 则表示程序是正常退出的,退出代码是告诉解释器的(或操作系统)
exit(1) 表示发生了错误进行退出
可以用不一样的数字表示不同的错误结果。比如你可以用exit(100) 来表示另一种和 exit(2)或 exit(1) 不同的错误
原文链接:https://blog.csdn.net/weixin_45938096/article/details/106747513
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。