- from random import randint
-
- class firstRoom():
- def __init__(self):
- print "Welcome to the start of the game. Good luck finishing it."
- print "There is a keypad on the door. You have to get all 3 digits correct, or the door will never open again."
- return 'break_out'
-
- def break_out(self):
-
- # Until everythin is working only one number
- combination = "%d" % (randint(1,3))
- guesses = 0
- guess = raw_input("[KEYPAD]>")
-
-
- while guess != combination and guesses < 10:
- print "INCORRECT, TRY AGAIN"
- guesses += 1
- return 'break_out'
-
- if guess == combination:
- print "welldone"
我没有收到任何错误,它只是什么都做不了。
顺便说一下,这是我第一次和班级一起工作。
# 回答1
它不应该做任何事情。您创建了一个类,但从未对其做过任何操作。
# 回答3
实例化类的变量,然后使用它。
- class TestClass:
- def f(self):
- return 'hello world'
-
- x = TestClass()
- x.f()
# 回答4
谢谢,但还是不能转到BREAKOUT功能吗?
# 回答6
正如Rabbit发布的那样,您必须调用该方法。
- >>> x = firstRoom()
- Welcome to the start of the game. Good luck finishing it.
- There is a keypad on the door. You have to get all 3 digits correct, or the door will never open again.
- >>> x.break_out()
- welldone
- >>>
# 回答7
我怀疑你拥有的所有地方
您真正想要的是self.Break_out()
- from random import randint
-
- class firstRoom():
- def __init__(self):
- print "Welcome to the start of the game. Good luck finishing it."
- print "There is a keypad on the door. You have to get all 3 digits correct, or the door will never open again."
-
- self.break_out()
-
- def break_out(self):
-
- # Until everythin is working only one number
- combination = "%d" % (randint(1,3))
- guesses = 0
- guess = raw_input("[KEYPAD]>")
-
-
- while guess != combination and guesses < 10:
- print "INCORRECT, TRY AGAIN"
- guesses += 1
- print combination, guesses # Debug added by me
- self.break_out()
-
- if guess == combination:
- print "welldone"
-
-
- if __name__ == "__main__":
- firstRoom()
-
如果是这样的话,您的代码中就存在另一个主要问题。每次你猜的时候,组合就会重置,就像你猜的一样。
编辑。我感到无聊,对程序进行了一些修改,并对其进行了扩展,以使猜测更公平一些。
- from random import randint
-
- class firstRoom():
-
- def __init__(self):
-
- print "Welcome to the start of the game. Good luck finishing it."
- print """There is a keypad on the door. You have to get all 3 digits
- correct, or the door will never open again."""
-
- # Until everythin is working only one number
- self.combination = "".join([str(randint(0,9)) for x in xrange(3)])
- self.guesses = 0
-
- def break_out(self):
-
- guess = raw_input("[KEYPAD]>")
-
- if guess != self.combination and self.guesses < 10:
-
- print "INCORRECT, TRY AGAIN"
- self.guesses += 1
- self.eval_guess(guess)
- self.break_out()
-
- elif guess == self.combination:
- print "welldone"
-
- else:
- print "FAIL!"
-
- def eval_guess(self, guess):
-
- # print self.guesses, self.combination # Debug
-
- if len(guess) == 3 and guess.isdigit():
- lg = []
- for x, y in zip(guess, self.combination):
- if int(x) < int(y):
- lg.append("<")
-
- elif int(x) > int(y):
- lg.append(">")
-
- else:
- lg.append("=")
-
- print "".join(lg)
-
-
- if __name__ == "__main__":
- x = firstRoom()
- x.break_out()