Guess my number problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • matusiek191
    New Member
    • Jul 2015
    • 4

    Guess my number problem

    I have a problem. For me all is correct but when im trying to have a first guess cmd dont print "print("Lower.. .")" and neither "print("Higher. ..")" statement. after first try everything works good. Help me just to find a problem because i cant see anything. Thank You

    Here is my source code:

    Code:
    import random
    
    print("\t\t\tWelcome to guess my number game!!!")
    print("Please enter the number between 1 and 100")
    
    number = random.randint(1, 100)
    guess = int(input("Take a guess: "))
    max_guess = 5
    tries = 1
    
        
    
    while guess != number:
        guess = int(input("Take a guess: "))
        tries += 1
       
        if guess > number:
            print("Lower...")
        elif guess < number:
            print("Higher...")
        else:
            print("Good you got it and it only tooked you ", tries, " tries!")
            break
    
        if tries == max_guess:
            print("\n\nYou are out of tries")
            break
    
        
    
    
    
    input("\n\nPress Enter to exit")
  • matusiek191
    New Member
    • Jul 2015
    • 4

    #2
    After changing " tries += 1 guess = int(input("Gues s again: "))" from the beggining to the end of the loop it works ok but after you guess the number cmd doesnt want to print "print("Goo d you got it and it only tooked you ", tries, " tries!")". Maybe this code is a mess but im new in this. Just need some more experience. Thanks for help.
    Code:
    import random
    
    print("\t\t\tWelcome to guess my number game!!!")
    print("Please enter the number between 1 and 100")
    
    number = random.randint(1, 100)
    guess = int(input("Take a guess: "))
    max_guess = 5
    tries = 1
        
    while guess != number:       
        if guess > number:
            print("Lower...")
        elif guess < number:
            print("Higher...")
        else:
            print("Good you got it and it only tooked you ", tries, " tries!")
            break
        guess = int(input("Guess again: "))
        tries += 1
    
        if tries == max_guess:
            print("\n\nYou are out of tries")
            break
    
        
    input("\n\nPress Enter to exit")

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      Move your first guess under the while statement. Initialize tries to 0. I modified your code for Python 2.7:
      Code:
      import random
       
      print "Welcome to guess my number game!!!"
      print "Please enter the number between 1 and 100"
       
      number = random.randint(1, 100)
      print number
      max_guess = 5
      tries = 0
       
      while True:
          guess = int(raw_input("Take a guess: "))
          tries += 1
          if guess > number:
              print "Lower..."
          elif guess < number:
              print "Higher..."
          else:
              print "Good you got it and it only tooked you", tries, "tries!"
              break
          if tries == max_guess:
              print "\n\nYou are out of tries"
              break
      
      raw_input("\n\nPress Enter to exit")

      Comment

      • matusiek191
        New Member
        • Jul 2015
        • 4

        #4
        Thanks, I using Python 3.4.3 but thank you. It was so easy that I feel stupid right now

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          You are welcome. Sometimes the obvious solution is the hardest to see.

          Comment

          Working...