364,111 Members | 2030 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

Why will this not do anything?

Daniel Johnson
P: 8
Expand|Select|Wrap|Line Numbers
  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"

I do not get any errors, it just doesn't do anything.

This is my first time working with classes by the way.
Jan 27 '12 #1

✓ answered by Smygis

I suspect that all the places you have
Expand|Select|Wrap|Line Numbers
  1. return 'break_out'
That what you really want is self.break_out()
Expand|Select|Wrap|Line Numbers
  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.  
And if so you have an other major problem in your code. Every time you guess the combination resets as well as guesses.

edit. I got bored and fixed the program up a bit as well as extending it to make the guessing a bit more fair.

Expand|Select|Wrap|Line Numbers
  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()
Share this Question
Share on Google+
7 Replies


Rabbit
Expert Mod 5K+
P: 6,668
It shouldn't do anything. You've created a class but you never do anything with it.
Jan 27 '12 #2

Daniel Johnson
P: 8
So how do i get it to work?
Jan 27 '12 #3

Rabbit
Expert Mod 5K+
P: 6,668
Instantiate a variable of the class and then use it.
Expand|Select|Wrap|Line Numbers
  1. class TestClass:
  2.    def f(self):
  3.       return 'hello world'
  4.  
  5. x = TestClass()
  6. x.f()
Jan 27 '12 #4

Daniel Johnson
P: 8
Thanks but it still doesn't go to the break_out function?
Jan 27 '12 #5

Rabbit
Expert Mod 5K+
P: 6,668
We need to see the code.
Jan 28 '12 #6

bvdet
Expert Mod 2.5K+
P: 2,509
As Rabbit posted, you have to call the method.
Expand|Select|Wrap|Line Numbers
  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. >>>
Jan 28 '12 #7

Smygis
100+
P: 121
I suspect that all the places you have
Expand|Select|Wrap|Line Numbers
  1. return 'break_out'
That what you really want is self.break_out()
Expand|Select|Wrap|Line Numbers
  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.  
And if so you have an other major problem in your code. Every time you guess the combination resets as well as guesses.

edit. I got bored and fixed the program up a bit as well as extending it to make the guessing a bit more fair.

Expand|Select|Wrap|Line Numbers
  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()
Jan 28 '12 #8

Post your reply

Help answer this question



Didn't find the answer to your Python question?

You can also browse similar questions: Python