简体   繁体   中英

How do I keep track of a user's guesses in Python? attempts= attempts + 1 is not working

I need to keep track of the number of guesses a user inputs in a simple guessing game.

I have tried using attempts= 0 and then setting attempts to = attempts + 1. Even when I do this, the code will print "You have guessed in 1 attempts" even when the user has guessed in more attempts than one.

Code:

attempts = 0;
print("Hello, welcome to the game. You will be choosing a number 
between 1 and 100. You can only guess up to 10 times.")

for tries in range(tries_allowed):
    print("You only get 10 tries.")
    break 

while attempts < 10:
    guess = int(input("Please guess a number"));
    attempts_used= attempts + 1;
    if guess > random_number:
            print("Guess is too high, try a smaller number");
    elif guess < random_number:
            print("Guess is too low, try a higher number");
    elif guess == random_number:
            attempts_used=str(attempts_used)
            print("Correct- you win in", attempts_used, "guesses");
            exit();
else:
    if tries_allowed == 10:
       print("You failed to guess in time")

my_list= [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
my_list.append(attempts_used)
print(my_list)

You never update the attempts variable, you've created a new one called attempts_used , you don't need to do this.

Just use attempts everywhere you're using attempts_used

Note: Whilst you're at it you should get rid of what is known as a "magic number", or a hard coded limit in your while loop

while attempts < tries_allowed:

Cleaned up your code a bit, shows the += counting method working for your script. As others have said, the original code is creating an entirely new variable attempts_used that is simply attempts + 1 , and attempts remains 0.

It could also be attempts = attempts + 1 , += means the same thing.

To make a int a str in python for printing purposes, it does not need to be stored to a separate variable, just call str() around it, unless you plan to use the string separately.

import random

random_number = random.randint(1,100)

attempts = 0
tries_allowed = 10
print("Hello, welcome to the game. You will be choosing a number between 1 and 100")

print("You only get " + str(tries_allowed) + " tries.")

my_list = []

while attempts < tries_allowed:
    guess = int(input("Please guess a number: "))
    if guess in my_list:
        print("You have already guessed " + str(guess))
        continue
    attempts += 1
    my_list.append(guess)
    if guess > random_number:
            print("Guess is too high, try a smaller number")
    elif guess < random_number:
            print("Guess is too low, try a higher number")
    elif guess == random_number:
            print("Correct- you win in", str(attempts), "guesses")
            break
else:
    if attempts == 10:
       print("You failed to guess in time")

for item in my_list:
    print(item)

您的“ attemps”变量保持为0,因此attemps_used(attempts + 1)始终为1。您必须将两个变量合并为一个变量才能对其进行控制(attempts = attempts + 1)

Code which also checks previous input and prints a message.

import random
# Hello World program in Python

attempts = 0
print("Hello, welcome to the game. You will be choosing a number between 1 and 100. You can only guess up to 10 times.")

random_number = random.randint(1, 101)
#print(random_number)
my_list= []

for tries in range(10):
    print("You only get 10 tries.")
    guess = int(input("Please guess a number: "))
    if guess in my_list:
        print("You already guessed this number!!")
    my_list.append(guess)
    attempts += 1

    if guess > random_number:
        print("Guess is too high, try a smaller number")
    elif guess < random_number:
        print("Guess is too low, try a higher number")
    else:
        print("Correct- you win in", tries + 1, "guesses")
        attempts = -1
        break


if attempts is 10 and attempts is not -1:   
    print("You failed to guess in time")
print("Attempts : ", my_list)

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