練習35 分支和函式

2021-07-14 18:29:00 字數 2571 閱讀 3448

# coding:utf-8

# ex35 分支和函式

from sys import exit

defgold_room

():print ("this room is full of gold. how much do you take?")

next = input("> ")

#原判斷**:

# if "0" in next or "1" in next:

# how_much = int(next)

# else:

# dead("man, learn to type a number.")

#新增的判斷,使用try當int()報錯時,提示輸入錯誤,否則進入對數字大小的判斷

try:

how_much = int(next)

except:

dead("wrong input.")

if how_much < 50:

print ("nice, you're not greedy, you win!")

exit(0)

else:

dead("you greedy bastard!")

defbear_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:

next = input("> ")

if next == "take honey":

dead("the bear looks at you then slaps your face off.")

elif next == "taunt bear"

andnot bear_moved:

print ("the bear has moved from the door. you can go through it now.")

bear_moved = true

elif next == "taunt bear"

and bear_moved:

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

elif next == "open door"

and bear_moved:

gold_room()

else:

print ("i got no idea what that means.")

defcthulhu_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?")

next = input("> ")

if"flee"

in next:

start()

elif

"head"

in next:

dead("well that was tasty!")

else:

cthulhu_room()

defdead

(why):

print (why, "good job!")

exit(0)

defstart

():print ("you are in a dark room.")

print ("there is a door to your right and left.")

print ("which one do you take?")

next = input("> ")

if next == "left":

bear_room()

elif next == "right":

cthulhu_room()

else:

dead("you stumble around the room until you starve.")

start()

# 加分題:

# 1.這個 gold_room 遊戲使用了奇怪的方式讓你鍵入乙個數字。這種方式會導致什麼樣的 bug?

# 你可以用比檢查 0、1 更好的方式判斷輸入是否是數字嗎?int() 這個函式可以給你一些頭緒。

# a:只檢查0,1在不在輸入內,當出現22,33,之類的數字就無法判斷正確性的。這邊使用int()搭配try方法使用,

# 給int()的引數不為數字時,會報錯,可以用這個來進行判斷是否為整數。

# 這個try的使用方法很基礎,僅作為解決本問題的方式,以後完善。

ex35 分支和函式

建立無限迴圈 next input if next take honey dead the bear looks at you then slaps your face off.elif next taunt bear andnot bear moved print the bear has mov...

習題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習題35分支和函式

這個學期的課太多了,我以後盡量做到至少一周一更吧,開始今天的學習吧 指令碼執行結果如下 這個指令碼 挺長的,截了兩次圖,不過這個文字編排做的挺好的,自動幫我右對齊了。這個 和之前的某一章挺像,也是乙個那種文字選擇遊戲。附加練習 1.把這個遊戲的地圖畫出來,把自己的路線也畫出來。答 這個能畫,不過可能...