简体   繁体   中英

How to tell user how many attempts left in Python

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!

print('attempts left: ', 6 - attempts)
print(6 - attempts, "attempts left")

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")

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 break s (ie out of guesses);
  3. Use if: elif: else: rather than separate if s; 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM