-1

I want to store a list of guesses the user has already made, so that when the user makes the next guess I can check that guess against a list of previous guesses. If the guess the user just made is in that list I want to tell the user to guess again and not count it as a attempt(5 attempts at guessing correct number)

tried using the append method to append the guesses to a blank list but I'm getting a "int obj has no append method" error.

import random

def guess_a_number():

    chances = 5
    random_number_generation = random.randint(1,21)

    while chances != 0:

        choice = int(input("Guess a number between 1-20, you only have {} chances left ".format(chances)))

        if choice > random_number_generation:
            print("Your number is too high, guess lower")
        elif choice < random_number_generation:
            print("Your number is too low, guess higher")
        else:
            print("You guessed the correct number!!!")
            break

        chances -= 1

        if chances == 0:
            try_again = input("Do you want to try play again? ")
            if try_again.lower() == "yes":
                guess_a_number()
            else:
                print("Better luck next time")


guess_a_number()
Prune
  • 76,765
  • 14
  • 60
  • 81
  • 1
    Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. [Minimal, complete, verifiable example](https://stackoverflow.com/help/minimal-reproducible-example) applies here. We cannot effectively help you until you post your MCVE code and accurately specify the problem. We should be able to paste your posted code into a text file and reproduce the problem you specified. Your posted code does not exhibit the problem you claim; there is no empty list and no append. – Prune Aug 20 '19 at 00:28
  • I suspect that you misused the `append` method -- check the documentation for the proper usage. `my_list.append(new_value)` – Prune Aug 20 '19 at 00:29
  • You should show the code where you try to append. It seems you may not be appending to a list but to an int which is not a supported operation. – Paul Rooney Aug 20 '19 at 00:48
  • Usually it is easier to show your broken code that you wrote try to solve the problem instead of a previous version of the code that works. – user202729 Aug 20 '19 at 01:22

2 Answers2

0

Try keeping a list of previous guesses and then check if guess in previous_guesses: immediately after the choice. You can use continue to skip the rest and prompt them again.

Seth Killian
  • 908
  • 9
  • 20
0

Just use a set or a list to hold the previously attempted numbers and check for those in the loop.

I think you already tried something similar but by the sound of it you were attempting to append to an int.

import random

while True:

    chances = 5
    randnum = random.randint(1, 21)
    prev_guesses = set()
    print("Guess a number between 1-20, you have {} chances ".format(chances))

    while True:

        try:
            choice = int(input("what is your guess? "))
        except ValueError:
            print('enter a valid integer')
            continue

        if choice in prev_guesses:
            print('you already tried {}'.format(choice))
            continue

        if choice > randnum:
            print("Your number is too high, guess lower")
        elif choice < randnum:
            print("Your number is too low, guess higher")
        else:
            print("You guessed the correct number!!!")
            break

        chances -= 1
        prev_guesses.add(choice)
        print("you have {} chances left".format(chances))

        if chances == 0:
            print("You ran out of guesses, it was {}".format(randnum))
            break

    try_again = input("Do you want to play again? ")

    if try_again.lower() not in ("y", "yes"):
        print("Better luck next time")
        break
Paul Rooney
  • 20,879
  • 9
  • 40
  • 61