Why will this not do anything?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Daniel Johnson
    New Member
    • Dec 2011
    • 8

    Why will this not do anything?

    Code:
    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"

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

    This is my first time working with classes by the way.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    It shouldn't do anything. You've created a class but you never do anything with it.

    Comment

    • Daniel Johnson
      New Member
      • Dec 2011
      • 8

      #3
      So how do i get it to work?

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        Instantiate a variable of the class and then use it.
        Code:
        class TestClass:
           def f(self):
              return 'hello world'
        
        x = TestClass()
        x.f()

        Comment

        • Daniel Johnson
          New Member
          • Dec 2011
          • 8

          #5
          Thanks but it still doesn't go to the break_out function?

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            We need to see the code.

            Comment

            • bvdet
              Recognized Expert Specialist
              • Oct 2006
              • 2851

              #7
              As Rabbit posted, you have to call the method.
              Code:
              >>> 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
              >>>

              Comment

              • Smygis
                New Member
                • Jun 2007
                • 126

                #8
                I suspect that all the places you have
                Code:
                return 'break_out'
                That what you really want is self.break_out( )
                Code:
                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()
                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.

                Code:
                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()
                Last edited by Smygis; Jan 28 '12, 08:39 PM. Reason: ops im a bit rusty

                Comment

                Working...