Python3練習題系列(04)

2022-03-02 13:39:25 字數 3449 閱讀 1713

製作乙個遊戲

函式、if_elif_else, while, exit

#bastard 英 ['bɑːstəd; 'bæst-] 美 ['bæstɚd] adj. 私生的 n. 私生子

#greedy 英 ['griːdɪ] 美 ['gridi] adj. 貪婪的;貪吃的;渴望的

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

"and

notbear_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.")

'''taunt 英 [tɔːnt] 美 [tɔnt] n. 嘲弄;譏諷 adj. 很高的 vt. 奚落;逗弄

'''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 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()

#insane 英 [ɪn'seɪn] 美 [ɪn'sen] adj. 瘋狂的;精神病的;極愚蠢的

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()

#stumble 英 ['stʌmb(ə)l] 美 ['stʌmbl] n. 絆倒;蹣跚而行 vi. 躊躇,蹣跚;失足;犯錯 vt. 使…困惑;使…絆倒

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?

> 60

you greedy bastard! good job!

systemexit: 0

來自《笨辦法學python》

Python3道基礎練習題

練習 輸入乙個整數n,判斷這個整數是否是素數 prime 素數是指只能被1 和自身整除的數 如 2 3 5 7 11.方法 用排除法.一但n能被2 n 1的數整除就不是素 數,否則就一定是素數 n int input 請輸入乙個整數 if n 2 print n,不是素數 exit 方法 1 fla...

python3 練習題 多級選單

多級選單 需求 1.現有省 市 縣3級結構,要求程式啟動後,允許使用者可依次選擇進入各子選單 2.可在任意一級選單返回上一級 3.可以在任意一級選單退出程式 所需新知識點 列表 字典 menu 商河 萊蕪 濟寧 微山 嘉祥 曲阜 陵城 防山 北京 永定路 中關村 昌平 南口 沙河 朝陽 三里屯 雙井...

Python3練習題系列(05) 設計和除錯規則

1.每乙個 if 語句 必須包含乙個else 2.如果這個else 永遠都不應該被執行到,因為它本身沒有任何意義,那你必須在else 語句後面使用乙個叫做die 的函式,讓它列印出錯誤資訊並且死給你看,這和上一節的習題類似,這樣你可以找到很多的錯誤。3.if 語句 的巢狀不要超過2 層,最好盡量保持...