- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我通过 LPTHW 的方式努力,并且在练习 41 的额外学分上卡住了:
Extra Credit:
- Add cheat codes to the game so you can get past the more difficult rooms.
- Instead of having each function print itself, learn about “doc string” style comments. Write the room description as doc comments, and change the runner to print them.
- Once you have doc comments as the room description, do you need to have the function prompt even? Have the runner prompt the user, and pass that in to each function. Your functions should just be if-statements printing the result and returning the next room.
据我所知,到目前为止使用这样的文档字符串是不受欢迎的,但这是我想出的:
from sys import exit
from random import randint
globvar = ''
def death():
quips = ["You died. You kinda suck at this.",
"Nice job, you died ...jackass.",
"Such a loser.",
"I have a small puppy that's better at this."]
print quips[randint(0, len(quips)-1)]
exit(1)
def central_corridor():
"""The Gothons of Planet Percal #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.
"""
action = globvar
if action == "shoot!":
print "Quick on the draw you yank out your blaster and fire it at the Gothon."
print "His clown costume is flowing and moving around his body, which throws"
print "off your aim. Your laser hits his costume but misses him entirely. This"
print "completely ruins his brand new costume his mother bought him, which"
print "makes him fly into an insane rage and blast you repeatedly in the face until"
print "you are dead. Then he eats you."
return 'death'
elif action == "dodge!":
print "Like a world class boxer you dodge, weave, slip and slide right"
print "as the Gothon's blaster cranks a laser past your head."
print "In the middle of your artful dodge your foot slips and you"
print "bang your head on the metal wall and pass out."
print "You wake up shortly after only to die as the Gothon stomps on"
print "your head and eats you."
return 'death'
elif action == "tell a joke":
print "Lucky for you they made you learn Gothon insults in the academy."
print "You tell the one Gothon joke you know:"
print "Lbhe zbgure vf fb sng, jura fur fvgf nebhaq gur ubhfr, fur fvgf nebhaq gur ubhfr."
print "The Gothon stops, tries not to laugh, then busts out laughing and can't move."
print "While he's laughing you run up and shoot him square in the head"
print "putting him down, then jump through the Weapon Armory door."
return 'laser_weapon_armory'
else:
print "DOES NOT COMPUTE!"
return 'central_corridor'
def laser_weapon_armory():
"""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
neutron bomb in its container. There's a keypad lock on the box
and you need the code to get the bomb out. If you get the code
wrong 10 times then the lock closes forever and you can't
get the bomb. The code is 3 digits.
"""
code = "%d%d%d" % (randint(1,9), randint(1,9), randint(1,9))
guess = globvar
guesses = 0
cheat = '007'
while guess != code and guess != cheat and guesses < 10:
print "BZZZZEDDD!"
guesses += 1
guess = raw_input("[keypad]> ")
if guess == code:
print "The container clicks open and the seal breaks, letting gas out."
print "You grab the neutron bomb and run as fast as you can to the"
print "bridge where you must place it in the right spot."
return 'the_bridge'
elif guess == cheat:
print "The container clicks open and the seal breaks, letting gas out."
print "You grab the neutron bomb and run as fast as you can to the"
print "bridge where you must place it in the right spot."
return 'the_bridge'
else:
print "The lock buzzes one last time and then you hear a sickening"
print "melting sound as the mechanism is fused together."
print "You decide to sit there, and finally the Gothons blow up the"
print "ship from their ship and you die."
return 'death'
def the_bridge():
"""You burst onto the Bridge with the netron 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.
"""
action = globvar
if action == "throw the bomb":
print "In a panic you throw the bomb at the group of Gothons"
print "and make a leap for the door. Right as you drop it a"
print "Gothon shoots you right in the back killing you."
print "As you die you see another Gothon frantically try to disarm"
print "the bomb. You die knowing they will probably blow up when"
print "it goes off."
return 'death'
elif action == "slowly place the bomb":
print "You point your blaster at the bomb under your arm"
print "and the Gothons put their hands up and start to sweat."
print "You inch backward to the door, open it, and then carefully"
print "place the bomb on the floor, pointing your blaster at it."
print "You then jump back through the door, punch the close button"
print "and blast the lock so the Gothons can't get out."
print "Now that the bomb is placed you run to the escape pod to"
print "get off this tin can."
return 'escape_pod'
else:
print "DOES NOT COMPUTE!"
return "the_bridge"
def escape_pod():
"""You rush through the ship desperately trying to make it to
the escape pod before 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?
"""
good_pod = randint(1,5)
guess = globvar
great_pod = 1
if int(guess) == good_pod:
print "You jump into pod %s and hit the eject button." % guess
print "The pod easily slides out into space heading to"
print "the planet below. As it flies to the planet, you look"
print "back and see your ship implode then explode like a"
print "bright star, taking out the Gothon ship at the same"
print "time. You won!"
exit(0)
elif int(guess) == great_pod:
print "You jump into pod %s and hit the eject button." % guess
print "The pod easily slides out into space heading to"
print "the planet below. As it flies to the planet, you look"
print "back and see your ship implode then explode like a"
print "bright star, taking out the Gothon ship at the same"
print "time. You won!"
exit(0)
else:
print "You jump into pod %s and hit the eject button." % guess
print "The pod escapes out into the void of space, then"
print "implodes as the hull ruptures, crushing your body"
print "into jam jelly."
return 'death'
ROOMS = {
'death': death,
'central_corridor': central_corridor,
'laser_weapon_armory': laser_weapon_armory,
'the_bridge': the_bridge,
'escape_pod': escape_pod
}
def runner(themap, start):
next = start
while True:
## Global variable in order to pass prompt value to rooms
global globvar
room = themap[next]
print "\n--------"
print room.__doc__
## Room dependent prompts
if room == laser_weapon_armory:
prompt = raw_input("[keypad]> ")
elif room == escape_pod:
prompt = raw_input("[pod #]> ")
elif room == death:
return 'death'
else:
prompt = raw_input("> ")
## Set raw_input from prompt to Global variable (globvar)
globvar = prompt
next = room()
runner(ROOMS, 'central_corridor')
除“return 'death'”外,一切正常。它只是在打印以下内容后退出:
--------
None
在靠近底部的 runner 函数中,我认为添加 'elif room == death:' 位会像没有它一样工作,它只是挂起等待 raw_input() 然后打印一个随机的 'quip',因为它应该在之后我按 ENTER。有人可以帮助我了解我所缺少的吗?任何帮助表示赞赏!
编辑感谢一些快速的反馈,这就是我得到的,尽管 rocksportrocker 的解决方案更简单:
def runner(themap, start):
next = start
while True:
## Global variable in order to pass prompt value to rooms
global globvar
room = themap[next]
## Room dependent prompts
if room == laser_weapon_armory:
print "\n--------"
print room.__doc__
prompt = raw_input("[keypad]> ")
elif room == escape_pod:
print "\n--------"
print room.__doc__
prompt = raw_input("[pod #]> ")
elif room == the_bridge:
print "\n--------"
print room.__doc__
prompt = raw_input("> ")
elif room == central_corridor:
print "\n--------"
print room.__doc__
prompt = raw_input("> ")
else:
pass
## Set raw_input from prompt to Global variable (globvar)
globvar = prompt
next = room()
最佳答案
death
没有文档字符串。如果 room==death
然后 print room.__doc__
打印 None
。在下面的 if/elifs 中,您从函数 runnner
返回,这导致程序结束。
如果您添加缺少的文档字符串并且您将 runner()
中的 return "death"
替换为简单的 "pass
”,或者省略相应的 elif
子句。
关于python - 艰难地学习 Python ex41 额外学分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7349097/
Elixir 文档 states那个 In addition to the Elixir file extension .ex, Elixir also supports .exs files for
明日方舟TW-EX-5突袭怎么打 TW-EX-5突袭打法详解 关卡难点:开局高压+必须抢夺留声机 必备干员:减速类辅助、夜莺/法抗盾/控制干员 打法思路: 开局扔推王或者其他强力先锋,只
明日方舟TW-EX-6突袭怎么打 TW-EX-6突袭打法详解 关卡难点:抢夺留声机 必备干员:三个先锋,地刺,减速类干员 打法思路 开局左侧扔下桃金娘和先锋,然后马上爆费,在下方扔下第二
明日方舟TW-EX-7突袭怎么打 TW-EX-7突袭打法详解 关卡难点 本关最大的难点就是敌人输出过高,突袭难度情况下高练度重装也扛不住敌人的输出,需要利用箱子的布置还有留声机的输出来解决敌人,
明日方舟TW-EX-8突袭怎么打 TW-EX-8突袭打法详解 关卡难点 关卡的翻车点有两个,一个是注意自爆兵自爆的地点一定要正好,尽量保证其自爆的地方不要波及留声机,否则影响很大,另外就是如何蒸
明日方舟TW-EX-6突袭怎么打 TW-EX-6突袭小火龙单核打法攻略 1、关卡难点 活动开启时间:07月09日16:00-07月30日03:59 关卡开启时间:07月16日16:00-07
所以我一直在研究神圣,它看起来很棒。不幸的是,我没有找到任何像我试图实现的多文件用例示例。 所以我有一个名为configuration.py的文件,它旨在包含不同的变量,这些变量将(使用神圣)插入到代
我说的是Try ... Catch ex as Exception .... End Try 如果我将异常传递给错误记录器,我应该传递异常对象 (ex) 还是 ex.ToString ?传递 ex.T
这是我最近在工作的遗留代码中遇到的一个简单的最佳实践问题。我找不到针对这个特定问题的良好引用,因为我们可以通过多种方式来实现这一目标。但是,我想知道实现这一目标的最佳和最有效的方法。下面我创建了一些发
在 C# 中,年轻的开发人员经常使用“throw ex”而不是“throw”来向父方法抛出异常。 示例: try { // do stuff that can fail } catch (Ex
我们使用下面的脚本作为 EC2 实例的用户数据。我不明白 -ex 选项的用途是什么? #!/bin/bash -ex yum update -y yum groupinstall -y "Web Se
这是 ls -R 命令的输出: .: compare.sh searchByFile.sh startup.sh temp.txt test.sh compare.sh~ search
我真的是编程新手,我正在构建一个关于运动和位置的应用程序。我无法从“词典”中获取特定项目,但“词典”并不是真正的词典。 基本上,我有一个 TableView ,其中包含从我的 Firebase Fir
所以。我正在制作一个论坛,并且我想使用 AngularJS 来构建它。 首先。 现在,我已经通过以下方式建立了与数据库的连接: prepare($sql)){ $query->bind_result(
关于泛型的两个简单问题。 下面两个函数定义是否相同? FunctionA(Exception ex); FunctionB(T ex) where T : Exception; 通用实现 (Funct
MSVC 10 和 MSVC 9 在编译我的异常框架时都生成了 4 级警告消息,尽管程序的行为似乎是正确的。异常框架相当庞大和复杂,但我已经设法将其归结为它的本质。这是一个完整的程序,您可以在 VS1
我的问题是:用 getMessage 或 toString 或两者都记录更好吗?考虑到开源引发的错误。看到评论中的问题,但没有得到答案。也许我错过了什么?不要介意记录其中之一的小性能影响,但除非有充分
当我尝试使用 create-react-app 创建一个新的 React 应用程序时,我得到以下信息: » npx create-react-app my-order Creating a new R
背景 我正在尝试将 2 个单独工作的功能结合起来,但在使它们协同工作时遇到了麻烦。 *1) 如 solr wiki 所述我可以标记一个特定的 fq,然后在我的 facet.field 中排除它。即使在
我正在尝试使用从this website下载的代码来学习 ruby 。 我被困在这一点上。 def test_you_dont_get_null_pointer_errors_when_calli
我是一名优秀的程序员,十分优秀!