笨办法学python 习题42 加分练习

3、创建一个新版本,里边使用两个 class,其中一个是 Map ,另一个是 Engine 。提示: 把 play 放到 Engine 里面。、

#coding=utf-8
from sys import exit
from random import randint

class Map(object):
    def __init__(self):       
        self.quips = ["You died. You kinda suck at this.","Your mom would be proud. If she were smarter.","Such a luser.","I hava a small puppy that‘s better at this."]

def death(self):
        print self.quips[randint(0,len(self.quips)-1)]
        exit(1)

def central_corridor(self):
        action = raw_input(">>> ")
        if action == "shoot!":
            return ‘death‘
        elif action == "dodge!":
            return ‘death‘
        elif action == "tell a joke":
            return ‘laser_weapon_armory‘
        else:
            return ‘central_corridor‘

def laser_weapon_armory(self):
        code = "%d%d%d"% (randint(1,9)),(randint(1,9)),(randint(1,9))
        guess = raw_input("[keypad]>>> ")
        guesses = 0        
        while guess !=code and guesses < 10:
            print "BZZZZEDDD!"
            guesses += 1
            guesses = raw_input("[keypad]>>> ")
        if guess == code:
            return ‘the_bridge‘
        else:
            return ‘death‘

def the_bridge(self):
        action = raw_input(">>> ")
        if action == "throw the bomb":
            return ‘death‘
        elif action == "slowly place the bomb":
            return ‘escape_pod‘
        else:
            return ‘the_bridge‘

def escape_pod(self):
        good_pod = randint(1,5)
        guess = raw_input("[pod #]>>> ")
        if int(guess) != good_pod:
            return ‘death‘
        else:
            exit(0)

class Engine(Map):
    def __init__(self,start):
        self.start = start
  
    def play(self):        
        next = self.start
        while True:
            print "\n-----------"
            room = getattr(self,next)
            next = room()

a_game = Engine("central_corridor")
a_game.play()

备注:初学小菜鸟,欢迎各位大神指教

原文地址:https://www.cnblogs.com/jion/p/8117027.html

时间: 2024-08-03 09:49:25

笨办法学python 习题42 加分练习的相关文章

笨办法学Python - 习题3: Numbers and Math

Exercise2是注释和井号 Comments and Pound Characters 具体详情请参考习题一,这里就不在做过多的赘述. 习题 3: 数字和数学计算 学习目标:了解Python中常用的算术运算符,并了解运算符之间的先后运算顺序 在各大常用的计算机语言中都有常见的算术运算符,Python也是大同小异,下面我们来了解一下Python中常见的算术运算符: 算术运算符 以下假设变量x = 10 ,y = 20 运算符 描述 实例 + 加 - 两个对象相加 x+y = 30 - 减 -

笨办法学Python - 习题8-10: Printing &amp; Printing, Printing

目录 1.习题 8: 打印,打印 2.习题 9: 打印,打印,打印 3.习题 10: 那是什么? 3.1.转义序列: 4.习题总结: 1.习题 8: 打印,打印 学习目标:继续学习 %r 的格式化输出. 习题八中的练习代码是: #! -*-coding=utf-8 -*- formatter = "%r %r %r %r %r " print formatter % (1, "hello", [1,2,3], (1,2,3), {"name":&

笨办法学python 习题14 优化过 遇到问题的请看

print "\t what's you name?"user_name = raw_input('>') from sys import argvscript, = argv prompt = '>' print "\t hi %s,I'm the %s script"%(user_name,script)print "\t I'd like to ask you a few questions" print "\t Do

笨办法学 Python (Learn Python The Hard Way)

最近在看:笨办法学 Python (Learn Python The Hard Way) Contents: 译者前言 前言:笨办法更简单 习题 0: 准备工作 习题 1: 第一个程序 习题 2: 注释和井号 习题 3: 数字和数学计算 习题 4: 变量(variable)和命名 习题 5: 更多的变量和打印 习题 6: 字符串(string)和文本 习题 7: 更多打印 习题 8: 打印,打印 习题 9: 打印,打印,打印 习题 10: 那是什么? 习题 11: 提问 习题 12: 提示别人

笨办法学Python 练习13和14

原题: 1 from sys import argv 2 3 script, first, second, third = argv 4 5 print "The script is called:", script 6 print "Your first variable is:", first 7 print "Your second variable is:", second 8 print "Your third variabl

习题 5: 更多的变量和打印 | 笨办法学 Python

一. 简述 “格式化字符串(format string)” -  每一次你使用 ' ’ 或 " " 把一些文本引用起来,你就建立了一个字符串. 字符串是程序将信息展示给人的方式. 二. 代码 1 #!usr/bin/env python 2 # -*- coding:utf-8 -*- 3 4 # Author: xixihuang 5 # Date : 2016/08/31 09:52 AM 6 # Desc : 习题5:更多的变量与打印 7 # 键入更多的变量并且将它们打印出来.这

《笨办法学python第三版》习题26,原错误代码及正确代码

#import ex25 1 def break_words(stuff): 2 """This function will break up words for us.""" 3 words = stuff.split(' ') 4 return words 5 6 def sort_words(words): 7 """Sorts the words.""" 8 return sor

笨办法学Python(十三)

习题 13: 参数.解包.变量 在这节练习中,我们将讲到另外一种将变量传递给脚本的方法(所谓脚本,就是你写的 .py 程序).你已经知道,如果要运行 ex13.py,只要在命令行运行 python ex13.py 就可以了.这句命令中的 ex13.py 部分就是所谓的"参数(argument)",我们现在要做的就是写一个可以接受参数的脚本. 将下面的程序写下来,后面你将看到详细解释. 1 from sys import argv 2 3 script, first, second, t

笨办法学Python(二十五)

习题 25: 更多更多的练习 我们将做一些关于函数和变量的练习,以确认你真正掌握了这些知识.这节练习对你来说可以说是一本道:写程序,逐行研究,弄懂它. 不过这节练习还是有些不同,你不需要运行它,取而代之,你需要将它导入到 python 里通过自己执行函数的方式运行. 首先以正常的方式 python ex25.py 运行,找出里边的错误,并把它们都改正过来.然后你需要接着下面的答案章节完成这节练习. 你应该看到的结果 这节练习我们将在你之前用来做算术的 python 编译器里,用交互的方式和你的.