标签:nic while 使用 and nsa greedy room his 中断
一 相关知识
1 exit()函数的一些用法
二 代码
ex35.py
1 from sys import exit 2 3 def gold_room(): # 定义gold_room函数,完成gold_room房间内的相关操作 4 print("This room is full of gold. How much do you take?") 5 print("Plesse input number of gold you want to take:") 6 7 choice = input("> ") # 用户自己输入要带走的黄金的数量 8 """ 9 if "0" in choice or "1" in choice: 10 how_much = int(choice) 11 else: 12 dead("Man, learn to type a number.") 13 """ 14 how_much = int(choice) 15 if 0 < how_much < 50: # 根据用户带走的黄金数量判断用户是否过关 16 print("Nice, you‘re not greedy,you win!") 17 exit(0) # 表示用户通关了,结束程序运行 18 else: 19 dead("You greedy bastard!") # 没过关,你太贪心了! 20 21 def bear_room(): # 定义bear_room函数,完成相关操作 22 print("There is a bear here.") 23 print("The bear has a bunch of honey.") 24 print("The fat bear is in front of another door.") 25 print("How are you going to move the bear?") 26 bear_moved = False # 一个判断条件,判断小熊是否会攻击玩家 27 28 while True: # 构建一个无限循环 29 """ 30 问题:用户怎么知道输入什么操作? 31 在实际场景中,这些操作不需要用户自己输入,通过鼠标点击游戏场景中的对应部分内容就相当于输入了 32 """ 33 choice = input("> ") # 获取用户的操作 34 35 if choice == "take honey": # 用户动了熊宝的蜂蜜,它肯定要去去抢啊,所以用户没通关 36 dead("The bear looks at you then slaps your face.") 37 elif choice == "taunt bear" and not bear_moved: # 用户第一次嘲弄小熊,小熊将用户带到了房间门口 38 print("The bear has moved from the door.") 39 print("You can go through it now.") 40 bear_moved = True # 将熊的状态改为攻击状态,下次有人再来欺负自己,就打他!!! 41 elif choice == "taunt bear" and bear_moved: # 用户嘲弄小熊,小熊开启防卫模式!!! 42 dead("The bear gets pissed off and chews your leg.") # 调用dead函数,结束程序,闯关失败 43 elif choice == "open door" and bear_moved: # 用户没有管小熊,而是打开了小熊旁边的门 44 gold_room() # 用户找到了黄金,调用gold_room函数,进入下一关 45 else: 46 print("I got no ides what that means.") 47 48 def cthulhu_room(): # 定义cthulhu_room,执行相关操作 49 print("Here you see the great evil Cthulhu.") 50 print("He, it, whatever stares at you and you go insane.") 51 print("Do you flee for your life or eat your head?") 52 53 choice = input("> ") 54 55 if "flee" in choice: # 用户选择逃命则进入返回上一场景 56 start() # 调用start()函数开始闯关 57 elif "head" in choice: # 用户被克苏鲁吞噬了头颅 58 dead("Well that was tasty!") # 闯关失败 59 else: 60 cthulhu_room() # 一直调用函数本身,直到进入下一关或闯关失败 61 62 def dead(why): # 用来表示闯关失败,输出一些信息后结束程序运行 63 print(why,"Good job!") 64 exit(0) 65 66 def start(): # 用户顺利开始闯关 67 print("You are a dark room.") 68 print("There is a door to your right and left.") 69 print("Which one do you take?") 70 71 choice = input("> ") # 获取用户的操作,去哪个房间 72 73 if choice == "left": # 选择左边则进入有小熊的房间 74 bear_room() 75 elif choice == "right": 76 cthulhu_room() # 选择右边则进入cthulu_room房间 77 else: 78 dead("You stumble around the room until you strave.") # 闯关失败 79 80 start() # 调用start()函数,表示开始游戏
执行结果(自己玩的,为了代码比较少,所以直接结束游戏了~):
1 PS E:\6_AI-Study\1_Python\2_code_python\02_LearnPythonTheHardWay> python ex35.py 2 You are a dark room. 3 There is a door to your right and left. 4 Which one do you take? 5 > right 6 Here you see the great evil Cthulhu. 7 He, it, whatever stares at you and you go insane. 8 Do you flee for your life or eat your head? 9 > eat head 10 Well that was tasty! Good job!
标签:nic while 使用 and nsa greedy room his 中断
原文地址:https://www.cnblogs.com/luoxun/p/13335226.html