learn python the hard way ex52 求大神解答

weixin_36799897 2017-10-14 11:58:51
本人编程小白, 最近在学learn python the hard way ex52, 遇到难题, 我本想用 '*' 来收集在'Laser Weapon Armory'中收集所有错误的输入,从而在web上出现'generic_death'的显示页面,可是,结果却变成了在'Laser Weapon Armory'页面中输入正确的值’999‘无法跳转到下一个场景中,求大神帮忙解答

#map.py
#-*- coding:UTF-8 -*-
from sys import exit
import random
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",
"""
The Gothons of Planet #25 have invaded your ship and destroyed your entire crew.
You are the last surviving member and your last mission is to get the neutron
destruct bomb from the Weapons Armory, put it in the bridge, and blow the ship up
after getting into an escape pod.

You're running down the central corridor to the Weapons Armory when a Gothon jumps
out, red scaly skin, dark grimy teeth, and evil clown costume flowing around his
hate filled body. He's blocking the door to the Armory and about to pull a weapon
to blast you.
""")


laser_weapon_armory = Room("Laser Weapon Armory",
"""
Lucky for you they made you learn Gothon insults in the academy.
You tell the one Gothon joke you know:
Lbhe zebgure vf fb sng, jura fur fvgf nebhag gur ubhfr, fur fvgr nebhaq gur buhfr.
The Gothon stops, tries not ro laugh, then busts out laughing and can't move.
While he's laughing you run up and shoot him square in the head
putting him down, then jump through the Weapon Armory door.

You do a dive roll into the Weapon Armory, crouch and scan the room
for more Gothons that might be hiding. It's dead quiet, too quiet.
You stand up and run to the far side of the room and find the
nuetron bomb in its container. There's a keypad lock on the box
and you need the code to get the bomb out. If you gete the code
wrong 10 times then the lock closes forever and you can't
get the bomb. The code is 3 digits.
""")

the_bridge = Room("The Bridge",
"""
The container clicks open and the seal breaks, letting gas out.
You grab the nuetron bomb and run as fast as you can to the
bridge where you must place it in the right spot.

You burst onto the Bridge with the nuetron destruct bomb
under your arm and surprise 5 Gothons who are trying to
take control of the ship. Each of them has an even uglier
clown costume than the last. They haven't pulled their
weapons out yet, as they see the active bomb under your
arm and don't want to set it off.
""")


escape_pod = Room("Escape Pod",
"""
You point your blaster at the bomb under your arm
and the Gothons pur their hands up and start to sweat.
You inch backward to the door, open it, and then carefully
place the bomb on the floor, pointing your blaster at it.
YOu then jump back through the door, punch the close button
and blast the lock so the Gothons can't get out.
get off this tin can.

You rush through the ship despreately trying to make it to
the escape pod befire the whole ship explodes. It seems like
hardly any Gothons are on the ship, so your run is clear of
interference. You get to the chamber with the escape pods, and
now need to pick one to take. Some of them could be damaged
but you don't have time to look. There's 5 pods, which one
do you take?
""")


the_end_winner = Room("The End",
"""
You jump into pod 2 and hit the eject button.
The pod easily sildes out into space heading to
the planet below. As it flies to the planet, you look
back and see your ship implodes then explode like a
bright star, taking out the Gothon ship at the same
time. You won!
""")


the_end_loser = Room("The End",
"""
You jump into a random pod and hit the eject button
The pod escapes out into the void of space, then
implodes as the hull tuptures, crushing your body
into jam jelly.
"""
)

escape_pod.add_paths({
'2': the_end_winner,
'*': the_end_loser
})

# 输入错误的内容得到的结果
generic_death = Room("death",
random.choice(["You died. You kinda suck at this.",
"Your mom would be proud...if she were smater.",
"Such a luser.",
"I have a small puppy that's better at this."
])
)

the_bridge.add_paths({
'throw the bomb': generic_death,
'slowly place the bomb': escape_pod
})

laser_weapon_armory.add_paths({
'999': the_bridge,
'*': generic_death
})

central_corridor.add_paths({
'shoot!': generic_death,
'dodge!': generic_death,
'tell a joke': laser_weapon_armory
})
START = central_corridor




#engine.py

#-*- coding:utf-8 -*-
import web
from gothonweb import map

urls = (
'/', 'Index',
'/game', 'GameEngine',
)
app = web.application(urls, globals())
render = web.template.render('templates/', base='bases'

# session
if web.config.get('_session') is None:

store = web.session.DiskStore('sessions')
session = web.session.Session(app, store, initializer={'room':None})
web.config._session = session
else:
session = web.config._session




class Index(object):
def GET(self):
session.room = map.START
web.seeother('/game')

class GameEngine(object):
def GET(self):
if session.room:
return render.current_scene(room = session.room)
else:
return render.you_die()


def POST(self):
i = web.input(action="None")

# 对输入进行判断
if session.room.name == "Laser Weapon Armory":
if i.action != '999 ':
session.room = session.room.go('*')
else:
session.room = session.room.go(i.action)
else:
session.room = session.room.go(i.action)
web.seeother('/game')

if __name__ == "__main__":
app.run()


#lay_out.html

$def with (content)
<html>
<head>
<title>Gothons From Planet Percal #25</title>
</head>
<body>

<p>
<img src="E:/python/project/webtest/templates/lizhi.jpg"/>
</p>
$:content
</body>
</html>




#current_scene.html

$def with (room)
<h1>$room.name</h1>

<pre>
$room.description
</pre>

<br/>
$if room.name == "death" or room.name == "The End":
<a href="/">Play Again?</a>

$else:
<form action='/game', method='POST'>
Action: <input type='text', name='action'/>
<br/>
<input type='submit'/>
<br/>
</form>




#you_die.html

<h1>You Die!</h1>
<p>Looks like you bit the dust.</p>
<p><a href="/">Play Again</a></p>



...全文
246 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_39210478 2018-09-24
  • 打赏
  • 举报
回复 1
这么长时间了,估计楼主也找到原因,不过既然看到了,就说一下:
# 对输入进行判断
if session.room.name == "Laser Weapon Armory":
if i.action != '999 ':
这个里面多了一个空格,‘999 ’!=‘999’当然不能正常跳转了。

像下面这样,就能够全部正常跳转了:
i = web.input(action=None)
if session.room:
if i.action:
session.room = session.room.go(i.action)
web.seeother('/game')

37,721

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧