找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 2915|回复: 4

[已回复] 请教lpthd习题43附加练习

2

主题

7

帖子

7

积分

贫民

积分
7
Riario14 发表于 2016-8-29 21:03:25 | 显示全部楼层 |阅读模式
  1. class LaserWeaponArmory(Scene):

  2.     def enter(self):
  3.         print "You do a dive roll into the Weapon Armory, crouch and scan the room"
  4.         print "for more Gothons that might be hiding.  It's dead quiet, too quiet."
  5.         print "You stand up and run to the far side of the room and find the"
  6.         print "neutron bomb in its container.  There's a keypad lock on the box"
  7.         print "and you need the code to get the bomb out.  If you get the code"
  8.         print "wrong 10 times then the lock closes forever and you can't"
  9.         print "get the bomb.  The code is 3 digits."
  10.         code = "%d%d%d" % (randint(1,9), randint(1,9), randint(1,9))
  11.         guess = raw_input("[keypad]> ")
  12.         guesses = 0

  13.         while guess != code and guesses < 10:
  14.             print "BZZZZEDDD!"
  15.             guesses += 1
  16.             guess = raw_input("[keypad]> ")

  17.         if guess == code:
  18.             print "The container clicks open and the seal breaks, letting gas out."
  19.             print "You grab the neutron bomb and run as fast as you can to the"
  20.             print "bridge where you must place it in the right spot."
  21.             return 'the_bridge'
  22.         else:
  23.             print "The lock buzzes one last time and then you hear a sickening"
  24.             print "melting sound as the mechanism is fused together."
  25.             print "You decide to sit there, and finally the Gothons blow up the"
  26.             print "ship from their ship and you die."
  27.             return 'death'
复制代码


1. 这个输密码的要猜11次。而且无论是修改guesses的初始值为大于0的数,或是修改循环的条件,只要 guess != code都是猜11次
  1.     def enter(self):
  2.         print "You do a dive roll into the Weapon Armory, crouch and scan the room"
  3.         print "for more Gothons that might be hiding.  It's dead quiet, too quiet."
  4.         print "You stand up and run to the far side of the room and find the"
  5.         print "neutron bomb in its container.  There's a keypad lock on the box"
  6.         print "and you need the code to get the bomb out.  If you get the code"
  7.         print "wrong 10 times then the lock closes forever and you can't"
  8.         print "get the bomb.  The code is 3 digits."
  9.         code = "%d%d%d" % (randint(1,9), randint(1,9), randint(1,9))
  10.         print code #**************
  11.         guess = raw_input("[keypad]> ")
  12.         guesses = 0
复制代码

2. 三位随机数很难猜中,我想在脚本中打印出密码。为什么在code = "%d%d%d" % (randint(1,9), randint(1,9), randint(1,9))下一行用print code输出字符串,运行时会显示IndentationError: unexpected indent
回复

使用道具 举报

3

主题

7

帖子

7

积分

贫民

积分
7
HappyEnding 发表于 2016-8-29 21:25:15 | 显示全部楼层
IndentationError: unexpected indent 这样的错误一般都是格式没对齐吧
回复 支持 反对

使用道具 举报

2

主题

7

帖子

7

积分

贫民

积分
7
Riario14  楼主| 发表于 2016-8-29 21:35:55 | 显示全部楼层
HappyEnding 发表于 2016-8-29 21:25
IndentationError: unexpected indent 这样的错误一般都是格式没对齐吧

第9行 第10行 tab缩进对齐不对吗?
回复 支持 反对

使用道具 举报

2

主题

4

帖子

4

积分

贫民

积分
4
nopear 发表于 2016-8-29 22:32:50 | 显示全部楼层
题主:
问题一:
猜十一次是因为在循环之前已经有了一次输入,在循环内部从0到9又输入了十次。至于你所说的“而且无论是修改guesses的初始值为大于0的数,或是修改循环的条件”具体是什么意思我不太清楚,修改guesses的值是可以改变循环次数的。

问题二:
可能是% (randint(1,9), randint(1,9), randint(1,9)) 这个语句不能放在赋值语句之后,如果把这串移到print语句是可以输出code的。
贴一下我改过的脚本:
#        coding:utf-8
from random import randint
def enter():
    print "You do a dive roll into the Weapon Armory, crouch and scan the room"
    print "for more Gothons that might be hiding.  It's dead quiet, too quiet."
    print "You stand up and run to the far side of the room and find the"
    print "neutron bomb in its container.  There's a keypad lock on the box"
    print "and you need the code to get the bomb out.  If you get the code"
    print "wrong 10 times then the lock closes forever and you can't"
    print "get the bomb.  The code is 3 digits."
    code = "%d%d%d"
    print code %(randint(1,9), randint(1,9), randint(1,9)) #**************
    guess = raw_input("[keypad]> ")
    guesses = 0

enter()
回复 支持 反对

使用道具 举报

2

主题

7

帖子

7

积分

贫民

积分
7
Riario14  楼主| 发表于 2016-9-21 20:10:47 | 显示全部楼层
nopear 发表于 2016-8-29 22:32
题主:
问题一:
猜十一次是因为在循环之前已经有了一次输入,在循环内部从0到9又输入了十次。至于你所说的 ...

这样的话,code就是一个字符串%d%d%d而不是一个数。 guess无法和code作比较。 code可以用print直接输出,我之前的代码不行是以为,前一行使用4个空格缩进,print code用tab缩进,所以会报错
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表