《笨方法學Python》加分題35

 sys.exit 用於結束程序
 2 from sys import exit
 3 
 4 # 進入黃金房間後的邏輯
 5 def gold_room():
 6     print("This room is full of gold. How much do you take?")
 7 
 8     choice = input("> ")
 9     # 若是輸入不包含 0 或 1 則死
10     if "0" in choice or "1" in choice:
11         how_much = int(choice)
12     else:
13         dead("Man, learn to type a number.")
14 
15     # 若是輸入的數字大於等於 50 則死
16     if how_much < 50:
17         print("Nice, you're not greedy, you win!")
18         exit(0)
19     else:
20         dead("You greedy basterd!")
21 
22 # 實現熊房間的邏輯
23 def bear_room():
24     print("There is a bear here.")
25     print("The bear has a bunch of honey.")
26     print("The fat bear is in front of another door.")
27     print("How are you going to move the bear?")
28     bear_moved = False
29 
30     # 若是熊離開後直接開門就用不到 while 循環了.
31     while True:
32         # print(">>> bear_moved1 = ", bear_moved)
33         choice = input("> ")
34 
35         if choice == "take honey":
36             # print(">>> bear_moved2 = ", bear_moved)
37             dead("The bear looks at you then slaps your face off.")
38         elif choice == "taunt bear" and not bear_moved:
39             # print(">>> bear_moved3 = ", bear_moved)
40             print("The bear has moved from the door.")
41             print("You can go through it now.")
42             bear_moved = True
43         elif choice == "taunt bear" and bear_moved:
44             # print(">>> bear_moved4 = ", bear_moved)
45             dead("The bear gets pissed off and chews your legs off.")
46         elif choice == "open door" and bear_moved:
47             # print(">>> bear_moved5 = ", bear_moved)
48             gold_room()
49         else:
50             print("I go no idea what that means.")
51 
52 # 惡魔房邏輯
53 def cthulhu_room():
54     print("Here you see the great evil Cthulhu.")
55     print("He, it, whatever stares at you and you go insane.")
56     print("Do you flee for your life or eat your head?")
57 
58     choice = input("> ")
59 
60     # 二選一,不然惡魔放循環
61     if "flee" in choice:
62         start()
63     elif "head" in choice:
64         dead("Well that was tasty!")
65     else:
66         cthulhu_room()
67 
68 # 慘死函數
69 def dead(why):
70     print(why, "Good job!")
71     exit(0)
72 
73 # 啓動函數
74 def start():
75     print("You are in a dark room.")
76     print("There is a door to your right and left.")
77     print("Which one do you take?")
78 
79     choice = input("> ")
80 
81     if choice == "left":
82         bear_room()
83     elif choice == "right":
84         cthulhu_room()
85     else:
86         dead("You stumble around the room until you starve.")
87 
88 # 開始遊戲
89 start()

 

運行結果ide