笨办法学python 习题13

笨办法学python
13题开始就看不懂了啊_python吧_百度贴吧
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&签到排名:今日本吧第个签到,本吧因你更精彩,明天继续来努力!
本吧签到人数:0成为超级会员,使用一键签到本月漏签0次!成为超级会员,赠送8张补签卡连续签到:天&&累计签到:天超级会员单次开通12个月以上,赠送连续签到卡3张
关注:140,295贴子:
笨办法学python
13题开始就看不懂了啊收藏
任何编译器运行都有问题
达内python培训,0基础入学,python课程免费试听;python金牌讲师授课,企业项目实战!python培训就选达内,美国上市公司;毕业推荐就业,7万家合作企业.培训就选大机构!
有没有同学遇到过这个问题啊
本来想帮你看看,看见你ID就不想了!
直接上你的代码把··
在命令行运行,
要输入三个变量
登录百度帐号推荐应用笨办法学 Python
这本简单书的目的是让你起步编程。虽然书名说是“笨办法”,但其实并非如此. 所谓的“笨办法”是指本书教授的方式。这本书的教学方式就是按照我告诉你的方式去做一系列的练习,目的是通过重复练习掌握一种技能。这对于一些什么都不知道的初学者,在理解更复杂的科目之前获取基本能力是很有效的方法。这种方法适用于一切领域,从武术到音乐甚至基本的数学和阅读技巧。
这本书指导你在 Python 中通过练习和记忆等技巧慢慢建设和建立技能,然后应用它们解决越来越困难的问题。在这本书的最后,你需要拥有必要的工具开始进行更多复杂程序的学习。我喜欢告诉大家,我的书带给你们“编程黑带”。意思是说你知道的基础知识足够现在就开始学习编程。
如果你认真学习,利用好你的时间,并学会这些技能,你就可以学习编程。
我要感谢 Angela 在这本书前两个版本里对我的帮助。没有她的帮助,我可能根本不能完成这本书。她帮我完成了第一份书稿的编辑工作,同时在我写这本书的过程一直很支持我。
我还想感谢 Greg Newman 帮我制作了封面,Brian Shumate 为我完成了早期的网站设计,以及阅读了这本书并给我反馈帮助我修正这本书的所有人。
英文原文地址:python--笨方法学python 习题52 - 迪米特 - 博客园
随笔 - 168, 文章 - 0, 评论 - 0, 引用 - 0
笨方法学python是一本不错的python入门书籍。书的最后一节是一个web版的游戏程序,以下是程序代码:
1.项目的目录结构如下所示:skeleton\
    app.py
    map.py
    templates\
        layout.html
        show_room.html
        you_died.html
2.游戏地图结构 map.py其中Room类是游戏场景,包括场景名称 name,描述 description,通往下一场景的可能路径集合 paths。
定义了游戏中设计到的7个场景。
# -*- coding:utf-8 -*-
class Room(object):
def __init__(self, name, description):
self.name = name
self.description = description
self.paths = {}
def go(self, direction):
return self.paths.get(direction, None)
def add_paths(self, paths):
self.paths.update(paths)
central_corridor = Room("Central Corridor",
#25外星战机入侵了你的飞船,并且摧毁了你的部队。你是最后一个幸存者,你的最终任务是从武器室拿到炸弹,将炸弹放在驾驶舱,跳进分离仓之后,炸掉飞船。
你现在正在往武器室跑去,这时,一个外星人跳了出来,可怕的红色皮肤,暗淡肮脏的牙齿,丑陋的服装围绕着他那充满憎恶的身体。他正在关闭通往武器室的大门,并且掏
出武器对准了你。
laser_weapon_armory=Room("Laser Weapon Armory",
幸运的是你知道外星人是由意念组成的。
你讲了一个你知道的外星笑话。
&$%@#$#%#@*&^$%$@#^&@%~$@%%!@~^^@#
外星人停止了动作,试着不发出笑声,最终禁不住大笑起来,并且停不下来。
当他笑的时候,你跑上前去,一枪命中他的眉心,将他放倒。然后跳进武器室。
你一个前空翻进入武器室,仔细地检查武器室是否藏有更多的外星人。死一般的寂静,太安静了。
你站起来,跑向武器室的角落,在盒子里找到了炸弹。盒子上有一个键盘,你需要输入密码才能将炸弹取出来。如果你输错10次,炸弹将永远被锁住无法取出。密码是4位数&字。
the_bridge=Room("The Bridge",
盒子咔哒一声打开了,密封破裂,毒气逸出。
你一把抓起炸弹,尽可能快递跑向驾驶舱,你必须把炸弹安置在合适的地点。
你闯入驾驶舱,手臂下夹着炸弹,惊奇地发现有5个外星人正在试图控制飞船。他们中的每一个都比上一个拥有更加丑陋的服装。他们没有拿出武器,因为他们看见了你手臂&下的炸弹,并且不希望引爆它。
escape_pod=Room("Escape Pod",
你拉出手臂下炸弹的导火线,外星人们举起手来,开始流汗。
你缓慢地退向门口,打开门,然后小心翼翼地把炸弹放在地上,拉出导火线。然后跳出驾驶舱,按下关闭按钮,门锁住了,外星人们无法逃出去。
现在炸弹已经安放好了,你跑向逃生通道。
你不顾一切地冲过飞船,以确保在整个飞船爆炸之前到达分离仓。看起来没有外星人在船上了,所以几乎没有遇到任何阻碍。你成功到达分离仓,然后你需要选择一个。他们
中的一些已经被摧毁了,但是你没有时间去检查。总共有5个分离仓,你将选择哪一个呢?
the_end_winner=Room("The End",
你跳进了2号分离舱 ,按下发动按钮。分离舱很容易地向下方的地球滑去。当它飞向地球时,你往回看,发现你的飞船像一颗明亮的星星一样炸开了,同时炸掉了恶灵的飞船
。你赢了!
the_end_loser=Room("The End",
你随便跳进一个分离舱,按下发动按钮。分离舱逃向未知的空间,然后因为外壳破裂而爆炸,把你的身体压成肉酱。
escape_pod.add_paths({
'2':the_end_winner,
'*':the_end_loser
generic_death=Room("death","You died.")
the_bridge.add_paths({
'throw the bomb':generic_death,
'slowly place the bomb':escape_pod
laser_weapon_armory.add_paths({
'0132':the_bridge,
'*':generic_death
central_corridor.add_paths({
'shoot!':generic_death,
'dodge!':generic_death,
'tell a joke':laser_weapon_armory
START=central_corridor
3.游戏引擎设计游戏引擎,也就是我们的web应用程序入口,定义了起始场景。将场景作为参数传递给场景信息显示模板show_room.html,显示当前场景描述信息。玩家输入动作指令,提交表单后,根据当前场景以及输入的指令,确定下一场景并显示。因为Http协议的无状态性,当前场景可以通过两种方式传递:(1)通过web.py框架的session机制 (2)通过&input type="hidden" value=""&隐藏域的方式。这里我们选择第一种方式。
import web
from map import *
import pdb
'/game','GameEngine',
'/','Index',
app=web.application(urls,globals())
if web.config.get('_session') is None:
store = web.session.DiskStore('sessions')
session = web.session.Session(app,store,initializer={'room':None,'count':0})
web.config._session = session
session = web.config._session
render = web.template.render('templates/',base="layout")
class Index(object):
def GET(self):
session.room = START
#输入错误密码次数初始化
session.count = 0
web.seeother("/game")
class GameEngine(object):
def GET(self):
if session.room:
return render.show_room(room=session.room)
#场景为None,则跳转到death界面
return render.you_died()
def POST(self):
form = web.input(action=None)
if session.room and form.action:
result = session.room.go(form.action)
pdb.set_trace()
#如果你输错10次,炸弹将永远被锁住无法取出
if session.room.name=='Laser Weapon Armory' and (result==None or result.name=='death') and session.count&10:
#输入错误密码次数累计
session.count=session.count+1
session.room = result
web.seeother("/game")
if __name__=="__main__":
4.接着创建模板页,模板放在templates文件夹下.mkdir templates首先创建一个基础模板页 layout.html
$def with (content)
&title&Gothons From Planet Percal #25&/title&
场景信息显示页面 &show_room.html
$def with (room)
&h1&$room.name&/h1&
$room.description
$if room.name=="death":
&p&&a href="/"&Play Again?&/a&&/p&
&form action="/game" method="POST"&
-&input type="text" name="action"&
&input type="SUBMIT"&
场景为None失败,提示界面&you_died.html
&h1&You Died!&/h1&
&p&Looks like you bit the dust&/p&
&p&&a href="/"&Play Again&/a&&/p&
5.运行:python app.py1 def break_words(stuff):
"""This function will break up words for us."""
words = stuff.split(' ')
return words
6 def sort_words(words):
"""Sorts the words."""
return sorted(words)
10 def print_first_word(words)
"""Prints the first word after popping it off."""
word = words.poop(0)
print word
15 def print_last_word(words):
"""Prints the last word after popping it off."""
word = words.pop(-1
print word
20 def sort_sentence(sentence):
"""Takes in a full sentence and returns the sorted words."""
words = break_words(sentence)
return sort_words(words)
25 def print_first_and_last(sentence):
"""Prints the first and last words of the sentence."""
words = break_words(sentence)
print_first_word(words)
print_last_word(words)
31 def print_first_and_last_sorted(sentence):
"""Sorts the words then prints the first and last one."""
words = sort_sentence(sentence)
print_first_word(words)
print_last_word(words)
37 print "Let's practice everything."
38 print 'You\'d need to know \'bout escapes with \\ that do \n newlines and \t tabs.'
40 poem = """
41 \tThe lovely world
42 with logic so firmly planted
43 cannot discern \n the needs of love
44 nor comprehend passion from intuition
45 and requires an explantion
46 \n\t\twhere there is none.
49 print "--------------"
50 print poem
51 print "--------------"
53 five = 10 - 2 + 3 - 5
54 print "This should be five: %s" % five
56 def secret_formula(started):
jelly_beans = started * 500
jars = jelly_beans \ 1000
#除法是&/&
crates = jars / 100
return jelly_beans, jars, crates
62 start_point = 10000
63 beans, jars, crates == secret_formula(start-point)
# jelly_beans =
start_point
65 print "With a starting point of: %d" % start_point
66 print "We'd have %d jeans, %d jars, and %d crates." % (beans, jars, crates)
68 start_point = start_point / 10
70 print "We can also do that this way:"
71 print "We'd have %d beans, %d jars, and %d crabapples." % secret_formula(start_pont
start_point)
73 sentence = "All god\tthings come to those who weight."
#good things
75 words = ex25.break_words(sentence)
76 sorted_words = ex25.sort_words(words)
78 print_first_word(words)
79 print_last_word(words)
80 .print_first_word(sorted_words)
# 去掉print前面的 .
81 print_last_word(sorted_words)
82 sorted_words = ex25.sort_sentence(sentence)
83 prin sorted_words
85 print_irst_and_last(sentence)
# print print_first_and_last
print_first_a_last_sorted(senence)
#indent print first_a_last_sorted(sentence)
正确代码:
1 import ex25
2 def break_words(stuff):
"""This function will break up words for us."""
words = stuff.split(' ')
return words
7 def sort_words(words):
"""Sorts the words."""
return sorted(words)
11 def print_first_word(words):
"""Prints the first word after popping it off."""
word = words.pop(0)
print word
16 def print_last_word(words):
"""Prints the last word after popping it off."""
word = words.pop(-1)
print word
21 def sort_sentence(sentence):
"""Takes in a full sentence and returns the sorted words."""
words = break_words(sentence)
return sort_words(words)
26 def print_first_and_last(sentence):
"""Prints the first and last words of the sentence."""
words = break_words(sentence)
print_first_word(words)
print_last_word(words)
32 def print_first_and_last_sorted(sentence):
"""Sorts the words then prints the first and last one."""
words = sort_sentence(sentence)
print_first_word(words)
print_last_word(words)
38 print "Let's practice everything."
39 print 'You\'d need to know \'bout escapes with \\ that do \n newlines and \t tabs.'
41 poem = """
42 \tThe lovely world
43 with logic so firmly planted
44 cannot discern \n the needs of love
45 nor comprehend passion from intuition
46 and requires an explantion
47 \n\t\twhere there is none.
50 print "--------------"
51 print poem
52 print "--------------"
54 five = 10 - 2 + 3 - 6
55 print "This should be five: %s" % five
57 def secret_formula(started):
jelly_beans = started * 500
jars = jelly_beans / 1000
crates = jars / 100
return jelly_beans, jars, crates
63 start_point = 10000
64 jelly_beans, jars, crates = secret_formula(start_point)
66 print "With a starting point of: %d" % start_point
67 print "We'd have %d jeans, %d jars, and %d crates." % (jelly_beans, jars, crates)
69 start_point = start_point / 10
71 print "We can also do that this way:"
72 print "We'd have %d beans, %d jars, and %d crabapples." % secret_formula(start_point)
74 sentence = "All god things come to those who weight."
76 words = ex25.break_words(sentence)
77 sorted_words = ex25.sort_words(words)
79 print_first_word(words)
80 print_last_word(words)
81 print_first_word(sorted_words)
82 print_last_word(sorted_words)
83 sorted_words = ex25.sort_sentence(sentence)
84 print sorted_words
86 print print_first_and_last(sentence)
88 print print_first_and_last_sorted(sentence)
阅读(...) 评论()Copyright & 2017 .
All Rights Reserved.
沪ICP备号-1

我要回帖

 

随机推荐