How to add add hits matches with python

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • marcelloc88
    New Member
    • May 2010
    • 3

    How to add add hits matches with python

    How can I add hits and matches in this source code?

    Code:
    import random
    guessCount = 0
    num = random.randrange(10,99)
    print "Find the mistery number between 10 and 98"
    guess = input("Type in your guess: ")
    guessCount +=1
    while num != guess:
    	if guess < num:
    		print "Too low! Guess higher."
    	else:
    		print "Too high! Guess lower."
    	guess = input("Guess again: ")
    	guessCount = guessCount + 1
    	print ("Guesses: "), guessCount
    print "Correct!"
    Thanks :)
    Last edited by Niheel; May 10 '10, 11:30 AM. Reason: use code tags please [code] . . . [/code]
  • Glenton
    Recognized Expert Contributor
    • Nov 2008
    • 391

    #2
    What do you mean? I don't know if you can or not, but it's probably possible ;P

    Comment

    • marcelloc88
      New Member
      • May 2010
      • 3

      #3
      I mean. U guess a number. The mistery number is 62.
      you guess, 50. So, u will have 1 hit and 0 matches...
      If u don't get it tell me ;) I will explain it better ;)

      Comment

      • Glenton
        Recognized Expert Contributor
        • Nov 2008
        • 391

        #4
        It would probably also help if you used code tags, so we could actually see what was going on. Without indentation, it's pretty difficult to read python!

        But this might serve your purpose:
        Code:
        import random
        
        #Create an infinite loop for the games
        guessCount=0
        gameNo=0
        matches=0
        while True:
            gameNo+=1
            print "Game: ",gameNo
            num=random.randrange(10,100)
            print "Find the mystery number between 10 and 98"
            guess=input("Type in your guess (0 to exit): ")
            if guess==0: break
            guessCount+=1
            while num != guess:
                if guess < num:
                    print "Too low! Guess higher."
                elif guess > num:
                    print "Too high! Guess lower."
                guess=input("Guess again (0 to end game): ")
                if guess==0: break
                guessCount+=1
            else:
                print "Correct!"
                matches+=1
            print "Answer was ",num
            print "Guesses/hits: ", guessCount
            print "Matches: ", matches

        Comment

        • marcelloc88
          New Member
          • May 2010
          • 3

          #5
          Thank you so much, that helped! :)

          Comment

          Working...