为什么这没用?

选择 | 换行 | 行号
  1. from random import randint
  2.  
  3. class firstRoom():
  4.     def __init__(self):
  5.         print "Welcome to the start of the game. Good luck finishing it."
  6.         print "There is a keypad on the door. You have to get all 3 digits correct, or the door will never open again."
  7.         return 'break_out'
  8.  
  9.     def break_out(self):
  10.  
  11.         # Until everythin is working only one number
  12.         combination = "%d" % (randint(1,3))
  13.         guesses = 0
  14.         guess = raw_input("[KEYPAD]>")
  15.  
  16.  
  17.         while guess != combination and guesses < 10:
  18.             print "INCORRECT, TRY AGAIN"
  19.             guesses += 1
  20.             return 'break_out'
  21.  
  22.         if guess == combination:
  23.             print "welldone"

我没有收到任何错误,它只是什么都做不了。
顺便说一下,这是我第一次和班级一起工作。

# 回答1


它不应该做任何事情。您创建了一个类,但从未对其做过任何操作。
# 回答2


那么我怎么才能让它起作用呢?
# 回答3


实例化类的变量,然后使用它。

选择 | 换行 | 行号
  1. class TestClass:
  2.    def f(self):
  3.       return 'hello world'
  4.  
  5. x = TestClass()
  6. x.f()
# 回答4


谢谢,但还是不能转到BREAKOUT功能吗?
# 回答5


我们需要看看代码。
# 回答6


正如Rabbit发布的那样,您必须调用该方法。

选择 | 换行 | 行号
  1. >>> x = firstRoom()
  2. Welcome to the start of the game. Good luck finishing it.
  3. There is a keypad on the door. You have to get all 3 digits correct, or the door will never open again.
  4. >>> x.break_out()
  5. welldone
  6. >>>
# 回答7


我怀疑你拥有的所有地方

选择 | 换行 | 行号
  1. return 'break_out'

您真正想要的是self.Break_out()

选择 | 换行 | 行号
  1. from random import randint
  2.  
  3. class firstRoom():
  4.     def __init__(self):
  5.         print "Welcome to the start of the game. Good luck finishing it."
  6.         print "There is a keypad on the door. You have to get all 3 digits correct, or the door will never open again."
  7.  
  8.         self.break_out()
  9.  
  10.     def break_out(self):
  11.  
  12.         # Until everythin is working only one number
  13.         combination = "%d" % (randint(1,3))
  14.         guesses = 0
  15.         guess = raw_input("[KEYPAD]>")
  16.  
  17.  
  18.         while guess != combination and guesses < 10:
  19.             print "INCORRECT, TRY AGAIN"
  20.             guesses += 1
  21.             print combination, guesses # Debug added by me
  22.             self.break_out()
  23.  
  24.         if guess == combination:
  25.             print "welldone"
  26.  
  27.  
  28. if __name__ == "__main__":
  29.     firstRoom()
  30.  

如果是这样的话,您的代码中就存在另一个主要问题。每次你猜的时候,组合就会重置,就像你猜的一样。
编辑。我感到无聊,对程序进行了一些修改,并对其进行了扩展,以使猜测更公平一些。

选择 | 换行 | 行号
  1. from random import randint
  2.  
  3. class firstRoom():
  4.  
  5.     def __init__(self):
  6.  
  7.         print "Welcome to the start of the game. Good luck finishing it."
  8.         print """There is a keypad on the door. You have to get all 3 digits
  9.                 correct, or the door will never open again."""
  10.  
  11.         # Until everythin is working only one number
  12.         self.combination = "".join([str(randint(0,9)) for x in xrange(3)])
  13.         self.guesses = 0
  14.  
  15.     def break_out(self):
  16.  
  17.         guess = raw_input("[KEYPAD]>")
  18.  
  19.         if guess != self.combination and self.guesses < 10:
  20.  
  21.             print "INCORRECT, TRY AGAIN"
  22.             self.guesses += 1
  23.             self.eval_guess(guess)
  24.             self.break_out()
  25.  
  26.         elif guess == self.combination:
  27.             print "welldone"
  28.  
  29.         else:
  30.             print "FAIL!"
  31.  
  32.     def eval_guess(self, guess):
  33.  
  34.         # print self.guesses, self.combination # Debug
  35.  
  36.         if len(guess) == 3 and guess.isdigit():
  37.             lg = []
  38.             for x, y in zip(guess, self.combination):
  39.                 if int(x) < int(y):
  40.                     lg.append("<")
  41.  
  42.                 elif int(x) > int(y):
  43.                     lg.append(">")
  44.  
  45.                 else:
  46.                     lg.append("=")
  47.  
  48.             print "".join(lg)
  49.  
  50.  
  51. if __name__ == "__main__":
  52.     x = firstRoom()
  53.     x.break_out()

标签: python

添加新评论