這次是透過input語句, 來讓if, elif & else 去做判斷.
並不難.
要小心是分段!! 如果有多層次的判斷分支,
記得要TAB的空格一致
用Notepad++打下列的程式碼,
另存為ex31.py. 我附上中文注釋方便好讀.
print("""You enter a dark room with two doors.
Do you go through door #1 or door #2?""")
# 由input 輸入, 讓後面的語句來判斷,
door = input ("> ")
# 如果輸入為1, 就再執行一次選擇
if door == "1":
print("There's a giant bear here eating a cheese cake.")
print("What do you do?")
print("1. Take the cake.")
print("2. Scream at the dear")
# 再由input 輸入, 讓後面的語句來判斷,
bear = input("> ")
# 這行是1.1的判斷結果
if bear == "1":
print("The bear eats your face off, Good job!")
# 這行是1.2的判斷結果
elif bear == "2":
print("The bear eats yours legs off, Good job!")
# 這行是1.x的判斷結果
else:
print(f"Well, doing {bear} is probably better.")
print("Bear run away.")
# 如果輸入為2, 就再執行另一次選擇
elif door == "2":
print("You stare into the endless abyss at Cthulhu's retina.")
print("1. Blueberries.")
print("2. Yellow jacket clothespins.")
print("3. Understanding revolvers yelling melodies.")
# 再由input 輸入, 讓後面的語句來判斷
insanity = input("> ")
# 這行是2.1 & 2.2的判斷結果
if insanity == "1" or insanity == "2":
print("Your body survives powered by a mind of jello.")
print("Good job")
# 這行是2.x的判斷結果
else:
print("The insanity rots your eyes into a pool of muck.")
print("Good job!")
#這行是1.x & 2.x 都沒選的判斷結果.
else:
print("You stumble around and fall on a knife and die. Good job!")
然後用Windows的cmd, 執行python.
C:\Windows\System32>cd C:\Users\Peter\Desktop\Python\LP3THW C:\Users\Peter\Desktop\Python\LP3THW>python ex31.py You enter a dark room with two doors. Do you go through door #1 or door #2? > 1 There's a giant bear here eating a cheese cake. What do you do? 1. Take the cake. 2. Scream at the dear > 1 The bear eats your face off, Good job! C:\Users\Peter\Desktop\Python\LP3THW>
完成
最初發表 / 最後更新: 2018.09.09 / 2018.09.09

0 comments:
張貼留言