jeudi 9 mai 2019

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)

Aucun commentaire:

Enregistrer un commentaire