By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,922 Members | 1,689 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,922 IT Pros & Developers. It's quick & easy.

How to limit the amount of inputs(guesses)

P: 4
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.
Expand|Select|Wrap|Line Numbers
  1. n = 56
  3. while True:
  5.     guess = int(input("Guess a number: "))
  6.     if guess > n:
  7.         print("Guess lower")
  8.     elif guess < n:
  9.         print("Guess higher")
  10.     elif guess == n:
  11.         print("Congrats!, you have guessed the right number")
  12.         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.
Oct 28 '10 #1
Share this Question
Share on Google+
2 Replies

Expert 100+
P: 624
Instead of "while True", use a counter.
while ctr < max_num:
Oct 28 '10 #2

P: 4
I haven't seen ctr yet but I tried it and it worked.
Thank you
Oct 28 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.