How to limit the amount of inputs(guesses)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Azel Mr G
    New Member
    • Oct 2010
    • 4

    How to limit the amount of inputs(guesses)

    Hello guys and thank you in advance for reading my post.

    I am writing the following module which will ask the end-user to guess a number and after x amount of guesses a message should appear to says "Sorry, you have tried x times already"
    This is what I have so far.
    Code:
    n = 56
    
    while True:
            
        guess = int(input("Guess a number: "))
        if guess > n:
            print("Guess lower")
        elif guess < n:
            print("Guess higher")
        elif guess == n:
            print("Congrats!, you have guessed the right number")
            break
    I don't know how to limit the amount of guesses. can anyone explain how that can be accomplished?

    I am starting to learn the while loop.

    Thank you again.
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    Instead of "while True", use a counter.
    while ctr < max_num:

    Comment

    • Azel Mr G
      New Member
      • Oct 2010
      • 4

      #3
      I haven't seen ctr yet but I tried it and it worked.
      Thank you

      Comment

      Working...