1

I am trying to make a random number game in python where the computer has to generate a number between 1 and 20 and you have to guess it. I have limited the amount of guesses to 6. How do I print how many guesses the user has left when they get a guess wrong? Here is my code:

import random

attempts = 0

name = input("What is your name? ")
random = random.randint(1, 20)
print(name + ",","I'm thinking of a number between 1 and 20, What is it?")

while attempts < 6:
    number = int(input("Type your guess: "))
    attempts = attempts + 1
    int(print(attempts,"attemps left")) #This is the code to tell the user how many attempts left
    if number < random:
        print("Too low. Try something higher")
    if number > random:
        print("Too high. Try something lower")
    if number == random:
        break
if number == random:
    if attempts <= 3:
        print("Well done,",name + "! It took you only",attempts,"attempts")
    if attempts >= 4:
        print("Well done,",name + "! It took you",attempts,"attempts. Athough, next time try to get three attempts or lower")
if number != random:
    print("Sorry. All your attempts have been used up. The number I was thinking of was",random)

Thanks, Any help is greatly appreciated!

16.uk
  • 569
  • 3
  • 9
  • 19

4 Answers4

2
print('attempts left: ', 6 - attempts)
timgeb
  • 76,762
  • 20
  • 123
  • 145
1
print(6 - attempts, "attempts left")
DAXaholic
  • 33,312
  • 6
  • 76
  • 74
1

Your attempts variable counts the number of attempts used. Since 6 is the limit, 6 - attempts is the number of attempts left:

print(6 - attempts, "attempts left")

(No need to wrap this in an int call. I don't know why you did that.)

Incidentally, writing 6 for the maximum attempts all the time may obscure what the 6 means and make it hard to find all the places that need changing if you want to change the limit to, say, 7. It may be worth making a variable with a descriptive name:

max_attempts = 6
...
while attempts < max_attempts:
    ...
    print(max_attempts - attempts, "attempts left")
user2357112
  • 260,549
  • 28
  • 431
  • 505
  • Also, do you know how I can check if the user is inputting a int? And if they aren't then display "Please input a number"? – 16.uk May 24 '14 at 06:59
  • @ExTrEeMeO: http://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response – user2357112 May 24 '14 at 07:02
1

I would make four suggestions, which work towards making your code cleaner and simpler:

  1. Factor out the "magic number" 6 and count down from it, rather than up to it;
  2. Use for rather than while, so you don't have to increment/decrement the number of guesses manually, and use the else to determine if the loop breaks (i.e. out of guesses);
  3. Use if: elif: else: rather than separate ifs; and
  4. Use str.format.

This would make the code something like:

attempts = 6
for attempt in range(attempts, 0, -1):
    print("You have {0} attempts left.".format(attempt))
    number = int(input(...))
    if number < random:
        # too low
    elif number > random:
        # too high
    else:
        if attempt > (attempts // 2):
            # great
        else:
            # OK
        break
else:
    # out of guesses
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437