《笨方法学Python》加分题35

 sys.exit 用于结束程序
 2 from sys import exit
 3
 4 # 进入黄金房间后的逻辑
 5 def gold_room():
 6     print("This room is full of gold. How much do you take?")
 7
 8     choice = input("> ")
 9     # 如果输入不包含 0 或 1 则死
10     if "0" in choice or "1" in choice:
11         how_much = int(choice)
12     else:
13         dead("Man, learn to type a number.")
14
15     # 如果输入的数字大于等于 50 则死
16     if how_much < 50:
17         print("Nice, you‘re not greedy, you win!")
18         exit(0)
19     else:
20         dead("You greedy basterd!")
21
22 # 实现熊房间的逻辑
23 def bear_room():
24     print("There is a bear here.")
25     print("The bear has a bunch of honey.")
26     print("The fat bear is in front of another door.")
27     print("How are you going to move the bear?")
28     bear_moved = False
29
30     # 如果熊离开后直接开门就用不到 while 循环了.
31     while True:
32         # print(">>> bear_moved1 = ", bear_moved)
33         choice = input("> ")
34
35         if choice == "take honey":
36             # print(">>> bear_moved2 = ", bear_moved)
37             dead("The bear looks at you then slaps your face off.")
38         elif choice == "taunt bear" and not bear_moved:
39             # print(">>> bear_moved3 = ", bear_moved)
40             print("The bear has moved from the door.")
41             print("You can go through it now.")
42             bear_moved = True
43         elif choice == "taunt bear" and bear_moved:
44             # print(">>> bear_moved4 = ", bear_moved)
45             dead("The bear gets pissed off and chews your legs off.")
46         elif choice == "open door" and bear_moved:
47             # print(">>> bear_moved5 = ", bear_moved)
48             gold_room()
49         else:
50             print("I go no idea what that means.")
51
52 # 恶魔房逻辑
53 def cthulhu_room():
54     print("Here you see the great evil Cthulhu.")
55     print("He, it, whatever stares at you and you go insane.")
56     print("Do you flee for your life or eat your head?")
57
58     choice = input("> ")
59
60     # 二选一,否则恶魔放循环
61     if "flee" in choice:
62         start()
63     elif "head" in choice:
64         dead("Well that was tasty!")
65     else:
66         cthulhu_room()
67
68 # 惨死函数
69 def dead(why):
70     print(why, "Good job!")
71     exit(0)
72
73 # 启动函数
74 def start():
75     print("You are in a dark room.")
76     print("There is a door to your right and left.")
77     print("Which one do you take?")
78
79     choice = input("> ")
80
81     if choice == "left":
82         bear_room()
83     elif choice == "right":
84         cthulhu_room()
85     else:
86         dead("You stumble around the room until you starve.")
87
88 # 开始游戏
89 start()

原文地址:https://www.cnblogs.com/python2webdata/p/10553820.html

时间: 2024-11-03 21:55:15

《笨方法学Python》加分题35的相关文章

笨方法学Python,Lesson 35, 36

Exercise 35 代码 from sys import exit  def gold_room():     print "This room is full of gold. How much do you take?"          choice = raw_input("> ")     if "0" in choice or "1" in choice:         how_much = int(c

笨方法学python(5)加分题

这篇对应的是习题17,更多文件操作 # -*- coding: utf-8 -*- #对文件更多操作复制A文件的内容到B文件 #from sys import argv from os.path import exists prompt = "> " from_file = raw_input("please input the filename where you want to copy from: >") #in_file = open(from_

笨方法学python(6)加分题--列表与字典的区别

he string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H N A P L S I I G Y I R And then read line by line: "PAHNAPLSIIG

笨方法学python(4)加分题

自己在看笨方法学python这本书,把自己觉得有学到东西的记下来,并不是每个习题都有记录 这次对应的是:习题 6: 字符串(string)和文本 这次只要是想说明一下,在print语句中,只要带有格式化字符的,会当作格式化来处理 脚本1: 结果1: 打出的结果没有%r,那是因为当作格式化处理了 脚本2: 结果2: 会报错,因为print joke_evaluation %hilarious 是格式化的标识 脚本3: 结果3:

Day 2 笨方法学Python

手打第25个练习,出错的地方有: def 定义后indent 4个空格,第一行空了以后,直接换行是跟上面对其的,但是运行时是错误的,我的解决方法是,重新手动空格4个: 还发现一个问题就是,中文解释,以前老是出错 # -*- coding : utf-8 -*- 网上看到的加上这个就可以了,但是我的还是出错.今天偶然在削微寒的博客http://www.cnblogs.com/xueweihan/的GIthub上找到了答案 #coding:utf-8 换成这个语句就可以了.以后,尽量每句都加上注释,

《笨方法学Python》加分题28

1 #!usr/bin/python 2 # -*-coding:utf-8-*- 3 4 True and True 5 print ("True") 6 False and True 7 print ("False") 8 1 == 1 and 2 == 1 9 print ("False") 10 "test" == "test" 11 print ("True") 12 1 ==

笨方法学python(1)加分题

1. 让你的脚本再多打印一行 2.让你的脚本只打印一行

笨方法学python(3)加分题

数学和数学计算 print 25+30/6 #25加上30除以6 和为39 print 100-25*3%4 #100减去25乘以3的积再除以4的余数,就是100-3=97 print 100%16 #100除以16的余数=4 print 1/4 #1除以4,然后因为是整数,所以四舍5入为0 print 1.0/4.0 ##1.0除以4.0,因为是浮点数,所以等于0.25 print 3+5<5+4 #判断语句,返回的值为布尔型,true or false 8<9 为 true

笨方法学Python(1)

习题 1: 第一个程序(略) Warning如果你来自另外一个国家,而且你看到关于 ASCII 编码的错误,那就在你的 python 脚本的最上面加入这一行:# -*- coding: utf-8 -*-这样你就在脚本中使用了 unicode UTF-8 编码,这些错误就不会出现了. 语法错误(SyntaxError) 习题2:注释和井号(略) 习题3:数字和数字计算 习题4:变量和命名 习题 5: 更多的变量和打印(格式化字符串) my_name = 'Zed A. Shaw'my_age =