练习45 源代码
from sys import exit #sys.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: #choice变量中的字符串含有字符串“0”或者字符串“1”时成立
how_much = int(choice) #将字符串转化为数字
else:
dead("Man,learn to type a number.") #调用代码后段的自定义函数dead()
if how_much < 50:
print("Nice, you're not greedy, you win!")
exit(0) #无错误退出
else:
dead("You greedy bastard!") #调用代码后段的自定义函数dead()
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 #先给变量bear_moved赋值布尔值False
while True: #while True 需要有能够跳出循环的方法,如break,否则会一直循环
choice = input("> ")
if choice == "take honey":
dead("The bear looks at you then slaps your face") #熊看着你并攻击你的脸
elif choice == "taunt bear" and not bear_moved: #taunt bear辱骂熊并且not bear_moved(False)同时为True是成立
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时,条件成立
dead("The bear gets pissed off and chews your leg.") #熊生气了,咬你的腿
elif choice == "open door" and bear_moved: #当open door且bear_moved = True时,成立
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 at you and you gou insane.") #无论什么盯着你看,你都会发疯。
print("Do you flee for your life or eat your head?")#你是逃命还是吃你脑袋?
choice = input("> ")
if "flee" in choice: #输入值的值包括flee(逃命)时条件成立
start() #自定义函数,又重新开始
elif "head" in choice:
dead("Well 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": #输入的choice变量为left时成立
bear_room() #调用自定义函数bear_room()
elif choice == "right": #输入的choice变量为right时成立
cthulhu_room() #调用自定义函数cthulhu_room()
else:
dead("You stumble around the room until you starve.")#你在房间里跌跌撞撞,直到饿死
start()
输出结果
注意:脚本代码在终端(cmd)运行,exit(0)未报错,在Spyder中直接运行报错,报错如下:
An exception has occurred, use %tb to see the full traceback.
SystemExit: 0
C:\Users\***>python C:\Users\***\Desktop\Python3_exercises\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?
> 1000
You greedy bastard! Good job!
知识点:
1. sys模块的exit方法:
sys.exit()会引发一个异常:SystemExit,如果这个异常没有被捕获,那么python解释器将会退出。如果有捕获此异常的代码,那么这些代码还是会执行。捕获这个异常可以做一些额外的清理工作。0为正常退出,其他数值(1-127)为不正常,可抛异常事件供捕获。
2.while True循环语句应用:
采用该语句的核心思想是如果出现错误的话,可以继续循环,直到出现跳出循环的语句,因此while True 语句中一定要有结束该循环的break语句,否则会一直循环下去的。
本节练习使用的结束语句为exit(0)———>无错误退出。
附加练习
1. 画一个这个游戏的流程图,并指出它是如何运转的。
2. 为你不理解的函数写上注释。
源代码已注释。
3.为游戏增加一些功能,同时使代码更加简化。
暂时没想法。
4. 这个 gold_room 让你输入数字的方式有点奇怪。这样做有哪些 bug ?你能改善我的代码吗?可以查查看 int() 的相关知识。
gold_room修改后代码如下:
def gold_room():
print("This room is full of gold. How much do you take?")
choice = int(input("> "))
# if "0" in choice or "1" in choice: #choice变量中的字符串含有字符串“0”或者字符串“1”时成立
# how_much = int(choice) #将字符串转化为数字
# else:
# dead("Man,learn to type a number.") #调用代码后段的自定义函数dead()
if choice < 50:
print("Nice, you're not greedy, you win!")
exit(0) #无错误退出.
else:
dead("You greedy bastard!") #调用代码后段的自定义函数dead()
**int()方法:**字符串转化为数字格式。
常见问题
1.为什么你要用 while True? 这样可以构建一个无限循环。但需要有退出的方式。
2. exit(0) 是干什么用的?(同知识点1)。 在很多操作系统中,一个程序可以用 exit(0) 来结束,其中传入的数字代表是否有错误。如果你用 exit(1) 代表有 1 个错误, exit(0) 则代表程序正常退出。它不同于通常的布尔逻辑(0==False),因为你可以用不同的数字来表示不同的错误结果。你可以用 exit(100) 来表示与 exit(2) 或者 exit(1) 不同的错误结果。
3.为什么 input() 有时会被写成 input('> ')? 用于提示用户input 的参数是一个字符串,输入字符串,也可以在input中输入具体内容提示用户。