用Notepad++打下列的程式碼,
另存為ex35.py. 我附上中文注釋方便好讀.
from sys import exit # 定義一個函數叫gold_room. def gold_room(): print("This room is full of gold. How much do you take?") # 當輸入數字, 就繼續. # 當輸入非數字, 就跑dead函數. choice = input("> ") if "0" in choice or "1" in choice: how_much = int(choice) else: dead("Man, learn to type a number") # 當輸入數字小於50, 就跑exit函輸. # 當輸入數字大於50, 就跑dead函數. if how_much < 50: print("Nice, you're not greedy, you win!") exit(0) else: dead("You greedy bastard!") # 定義一個函數叫bear_room, 並印出來下面四行. # 然後定義bear_moved為False. 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 # 進入一個名為True的循環. while True: choice = input("> ") # 當輸入take honey, 就跑dead函數. if choice == "take honey": dead("The bear looks at you then slaps your face off.") # 當輸入taunt bear, 則定義bear_moved為True, 再重新循環一次. elif choice == "taunt bear" and not bear_moved: print("The bear has moved from the door.") print("You can go through it now.") bear_moved = True # 當輸入taunt bear, 且and bear_moved為True, 就跑dead函數. elif choice == "taunt bear" and bear_moved: dead("The bear gets pissed off and chews your leg off.") # 當輸入open door, 且and bear_moved為True, 就跑gold_room函數. elif choice == "open door" and bear_moved: gold_room() else: print("I got no idea what that means.") # 定義一個函數叫cthulhu_room, 並印出來下面三行. def cthulhu_room(): print("Here you see the great evil Cthulhu.") print("He, it, whatever stares at you and you go insane.") print("Do you flee for your life or eat your head?") choice = input("> ") # 當輸入flee, 就跑start函數. if "flee" in choice: start() # 當輸入head, 就跑dead函數. elif "head" in choice: dead("Well that was tasty!") # 除了以上兩個, 就再跑一次cthulhu_room函數. else: cthulhu_room() # 定義一個函數叫dead, 並印出來參數(why)跟Good job! def dead(why): print(why, "Good job!") exit(0) # 定義一個函數叫start, 並印出來下面三行. 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("> ") # 當輸入left, 就跑bear_room函數. if choice == "left": bear_room() # 當輸入right, 就跑cthulhu_room函數. elif choice == "right": cthulhu_room() # 除了以上兩個輸入, 就跑dead函數. else: dead("you stumble around the room until you starve.") # 開始跑start函數 start()
然後用Windows的cmd, 執行python.
C:\Windows\System32>cd C:\Users\Peter\Desktop\Python\LP3THW C:\Users\Peter\Desktop\Python\LP3THW>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? > 10 Nice, you're not greedy, you win! C:\Users\Peter\Desktop\Python\LP3THW>
完成
最初發表 / 最後更新: 2018.09.15 / 2018.09.15
0 comments:
張貼留言