笨办法学Python(learn python the hard way)--练习程序31-35

下面是练习31-练习35,基于python3

 #ex31.py
1
print("You enter a dark room witn two doors. Do you go through door #1 or door #2?") 2 3 4 door = input("> ") 5 6 if door == "1": 7 print("There's a giant bear here eating a cheese cake. What do you do?") 8 print("1. Take the cake.") 9 print("2. Scream at the bear.") 10 11 bear = input("> ") 12 13 if bear == "1": 14 print("The bear eats your face off. Good job!") 15 elif bear == "2": 16 print("The bear eats your legs off. Good job!") 17 else: 18 print("Well, doing %s is probably better. Bear runs away." %bear) 19 20 21 elif door == "2": 22 print("You stare into the endless abyss at Cthulhu's retina.") 23 print("1. Blueberries.") 24 print("2. Yellow jacket clothespins.") 25 print("3. Understanding revolvers yelling melodies.") 26 27 insanity = input("> ") 28 29 if insanity == "1" or insanity == "2": 30 print("Your body survives powered by a mind of jello. Good job!") 31 else: 32 print("The insanity rots your eyes into a pool of muck. Good job!") 33 34 else: 35 print("You stumble around and fall on a knife and die. Good job!")

 

 #ex32.py
1
the_count = [1, 2, 3, 4, 5] 2 fruits = ['apples', 'oranges', 'pears', 'apricots'] 3 change = [1, 'pennies', 2, 'dimes', 3, 'quarters'] 4 5 # this first kind of for-loop goes through a list 6 for number in the_count: 7 print("This is count %d" % number) 8 9 # same as above 10 for fruit in fruits: 11 print("A ftuit of type: %s" % fruit) 12 13 # also we can go through mixed lists too 14 # notice we have to use %r since we don't know what's in it 15 for i in change: 16 print("I got %r" % i) 17 18 # we can also build lists, first start with an empty one 19 #elements = [] 20 21 elements = range(0, 6) 22 # then use the range function to do 0 to 5 counts 23 #for i in range(0, 6): 24 # print("Adding %d to the list." % i) 25 # append is a function that lists understand 26 # elements.append(i) 27 28 # now we can print them out too 29 for i in elements: 30 print("Element was: %d" % i)

 

 #ex33.py
1
i = 0 2 numbers = [] 3 4 while i < 6: 5 print("At the top i is %d" % i) 6 numbers.append(i) 7 8 i = i + 1 9 print("Numbers now: ", numbers) 10 print("At the bottom i is %d" % i) 11 12 13 print("The numbers: ") 14 15 for num in numbers: 16 print(num)

 

 #ex33+.py
1
# 加分习题 2 #函数里边的变量和脚本里边的变量之间是没有连接的 3 #所以定义函数必须返回numbers变量并重新给numbers赋值 4 def numbers_append(j,k): 5 i = 0 6 numbers = [] 7 while i < j: 8 print("At the top i is %d" % i) 9 numbers.append(i) 10 11 i = i + k 12 print("Numbers now: ", numbers) 13 print("At the bottom i is %d" % i) 14 return numbers 15 16 17 numbers = numbers_append(6, 1) 18 19 print("The numbers: ") 20 21 for num in numbers: 22 print(num)

 

#ex34.py
1
animals = ['bear', 'tiger', 'penguin', 'zebra'] 2 bear = animals[1] 3 print(bear)

 

 #ex35.py
1
#sys.exit() 引发一个 SystemExit异常,若没有捕获这个异常,Python解释器会直接退出;捕获这个异常可以做一些额外的清理工作。0为正常退出,其他数值(1-127)为不正常,可抛异常事件供捕获。 2 from sys import exit 3 4 def gold_room(): 5 print("This room is full of gold. How much do you take?") 6 7 next = input("> ") 8 if "0" in next or "1" in next: 9 how_much = int(next) 10 else: 11 dead("Man, learn to type a number.") 12 13 if how_much < 50: 14 print("Nice, you're not greedy, you win!") 15 exit(0) 16 else: 17 dead("You greedy bastard!") 18 19 20 def bear_room(): 21 print("There is a bear here.") 22 print("The bear has a bunch of honey.") 23 print("The fat bear is in front of another door.") 24 print("How are you going to move the bear?") 25 bear_moved = False 26 while True: 27 next = input("> ") 28 29 if next == "take honey": 30 dead("The bear looks at you then slaps your face off.") 31 32 elif next == "taunt bear" and not bear_moved: 33 print("The bear has moved from the door. You can go through it now.") 34 bear_moved = True 35 36 elif next == "taunt bear" and bear_moved: 37 dead("The bear gets pissed off and chews your leg off.") 38 39 elif next == "open door" and bear_moved: 40 gold_room() 41 42 else: 43 print("I got no idea what that means.") 44 45 46 47 def cthulhu_room(): 48 print("Here you see the great evil Cthulhu.") 49 print("He, it, whatever stares at you and you go insane.") 50 print("Do you flee for your life or eat your head?") 51 52 next = input("> ") 53 54 if "flee" in next: 55 start() 56 elif "head" in next: 57 dead("Well that was tasty!") 58 else: 59 cthulhu_room() 60 61 62 def dead(why): 63 print(why, "Good job!") 64 exit(0) 65 66 def start(): 67 print("You are in a dark room.") 68 print("There is a door to your right and left.") 69 print("Which one do you take?") 70 71 next = input("> ") 72 73 if next == "left": 74 bear_room() 75 elif next == "right": 76 cthulhu_room() 77 else: 78 dead("You stumble around the room until you starve.") 79 80 81 start()

 

转载于:https://www.cnblogs.com/xiyouzhi/p/9600478.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值