《笨方法學 Python 3》35 分支和函式

2021-08-28 15:13:19 字數 2846 閱讀 5833

from sys import exit

def gold_room():

print("this room is full of gold. how much do you take?///這個房間裡滿是金子。你要多少錢?")

choice = input("> ")

if "1" or "0" in choice:

how_much = int(choice)

else:

dead("man, learn to type a number.///夥計,學會打乙個數字。")

if how_much <50:

print("nice, you're not greedy, you win!///很好,你不貪心,你贏了!")

exit(0)

else:

dead("you greedy bastard!///你個貪婪的混蛋!")

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

while true:

choice = input("> ")

if choice == "take honey":

dead("the bear looks at you then slaps your face off.///熊看著你,然後把你的臉打掉。")

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

elif choice == "open door" and not bear_moved:

dead("the bear gets pissed off and chews your face off.")

elif choice == "taunt bear" and bear_moved:

dead("the bear gets pissed off and chews your leg off.///熊很生氣,把你的腿咬掉。")

elif choice == "open door" and bear_moved:

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 starts at you and you go insane.///不管你從**開始,你都是極愚蠢的。")

print("do you flee for your life or eat your head?///你是為了你的生命而逃跑還是讓它吃你的頭?")

choice = input("> ")

if "flee" in choice:

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 you right and left.///你的左右手各有一扇門。")

print("which one do you take?///你選哪乙個?")

choice = input("> ")

if choice == "left":

bear_room()

elif choice == "right":

cthulhu_room()

else:

dead("you stumble arount the room untill you starve.///你在房間四處徘徊,直到餓死。")

start()

1. 遊戲路線:去左邊房間→嘲諷熊→開啟門→49金幣

注意:**中有乙個while true,它可以建立乙個無限迴圈的**塊,而在這個語句中,終止迴圈的方法有很多,只要呼叫了dead函式和gold_room函式就會終止,只要未呼叫這兩個函式,那它就會一直執行下去。

注意:gold_room 函式中 有一段判斷**:if "1" or "0" in choice ,用來判斷輸入是否為數字的,這個方法有段不太明白,還有另外的方法可以用來判斷是否為數字:if choice.isdigit()

習題35,分支和函式,笨方法學python

coding utf 8 from sys import exit defgold room print this room is full of gold.how much do you take?next raw input if 0 in next or 1 in next how much ...

笨方法學python

書結構非常簡單,其實就是 52 個習題而已。其中 26 個覆蓋了輸入輸出 變數 以及函式三個課題,另外 26 個覆蓋了一些比較高階的話題,如條件判斷 迴圈 類和物件 測試 以及專案的實現等。每一章節的格式基本都是一樣的,以 練習題開始,讀者照著說明 編寫 不允許複製貼上 執行並檢查結果,然後再做一下...

笨方法學python 一

print i like typing this print this is fun print i said do not touch this.如果用同種引號會報錯 file ex1.py line 8 print i said do not touch this.syntaxerror inv...