Home > Mobile >  How do I store the results of a function into a list of some sort over time in Python?
How do I store the results of a function into a list of some sort over time in Python?

Time:10-27

So I'm refreshing what little I knew of Python before and playing around with some beginner projects. I'm toying arond with it now, and I'm just trying to learn and see what I can do. I made a "Guessing Game" and turned it into a function. I want to store these reults in a list each time it is used. I want the results to automatically go to the list when the game is completed and then to be able to print the list when desired.

I'm not sure if I need to create a new function for this, or if I should be creating this within my current "guessing_game" function. I've tried to create a list previously, but I'm not sure how to create and store the variable of the game result in order to add it into the list. I feel like this is probably a fairly simple problem, so I apologize if this is a dumb question.

def guessing_game():
    import random

    number = random.randint(1, 1000)
    player_name = input("Enter name ")
    number_of_guesses = 0


    print('Howdy'   player_name   "Guess a number between 1 and 1000: ")

    while number_of_guesses < 10:
        guess = int(input())
        number_of_guesses  = 1
        if guess < number:
            print("Too Low, Joe")
        if guess > number:
            print("Too high, Sly")
        if guess == number:
            break

    if guess == number:
        print("You got it, Bobbit, in "   str(number_of_guesses)   " tries")
    else:
        print(" Well, yer were close, Stofe. Shoulda guessed "   str(number))

print(guessing_game())





CodePudding user response:

You can create a list inside of the function, and then everytime they guess, you can store the guess inside the list. At the end, we can print the list.

def guessing_game():
    import random

    number = random.randint(1, 1000)
    player_name = input("Enter name: ")
    number_of_guesses = 0
    guesses = []


    print('Howdy '   player_name   " Guess a number between 1 and 1000: ")

    while number_of_guesses < 10:
        guess = int(input())
        guesses.append(guess)
        number_of_guesses  = 1
        if guess < number:
            print("Too Low, Joe")
        if guess > number:
            print("Too high, Sly")
        if guess == number:
            break

    if guess == number:
        print("You got it, Bobbit, in "   str(number_of_guesses)   " tries")
    else:
        print(" Well, yer were close, Stofe. Shoulda guessed "   str(number))
    
    print("These were the numbers you guessed:")
    for g in guesses:
        print(g)

print(guessing_game())

CodePudding user response:

What you need to do is create a list with something like val = []. I added it into the code and also added a formatting piece so it looks nice.

def guessing_game():
    import random
    guesses = []
    number = random.randint(1, 1000)
    guess = number / 2
    player_name = input("Enter name ")
    number_of_guesses = 0

    print(f'Howdy {player_name}. Guess a number between 1 and 1000: ')

    while number_of_guesses < 10:
        guess = int(input('> '))
        guesses = guesses   [guess]
        number_of_guesses  = 1
        if guess < number:
            print("Too Low, Joe")
        if guess > number:
            print("Too high, Sly")
        if guess == number:
            break
    formatted_guesses = ''
    for _ in range(len(guesses)):
        if _ != len(guesses) - 1:
            formatted_guesses = formatted_guesses   str(guesses[_])   ', '
        else:
            formatted_guesses = formatted_guesses   str(guesses[_])   '.'
    if guess == number:
        print("You got it, Bobbit, in "   str(number_of_guesses)   " tries")
        print(f'You guessed {formatted_guesses}')
    else:
        print("Well, yer were close, Stofe. Shoulda guessed "   str(number))
        print(f'You guessed {formatted_guesses}')


guessing_game()
  • Related