Search code examples
pythoninputconsoleuser-input

How to get the console to "remember" a user input in Python?


I am trying to figure out how I can get the console to "remember" what the user has inputted into the console for my hangman game. For example, if the word "tree" is picked from the hangman word string that I created, if a user inputs the letter "r", I want the console to remember that input and keep printing it to the console every time the user inputs a letter until the session ends. I want the console to re-print "- r - -" if the user inputs "r" while the user can find the other letters in the word. I wish for the same thing to happen if a user inputs the letter "t" or "e" as well: "- r e e". Thank you, sorry if I am asking for too much, I can't find the answer to this.

Here is my code:

#hangman mini-project

import random
import string
import time

letters = string.ascii_letters
lettertree = ['a', 'b', 'c', 'd', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 's', 'u', 'v', 'w', 'x', 'y', 'z']
hangmanwords = ['tree','sun']
sunchoices = ['s _ _', '_ u _', '_ _ n']
treechoices = ['t _ _ _', '_ r _ _', ' _ _ e _', '_ _ _ e']
lettercount = [0]
gameWinTree = False
gameWinSun = False
limbCount = 5

hangmanword = random.choice(hangmanwords)
correct = hangmanword
if hangmanword == "sun":
    print (random.choice(sunchoices))
if hangmanword == "tree":
    print (random.choice(treechoices))
    
    
    
    if letters == "r":
        print("Nice! You guessed one letter from the word")
        if letters == "e":
            print("Wow! You guessed two letters from the word, if you wish, you can guess the word")
while True:
    letters = input("Please enter a letter to guess the word")
    if letters == correct:
        input("Correct! The word was " + correct + ". Press enter to play again.")
        time.sleep(1)
        break
    if letters == correct:
        gameWinTree == true
        if gameWinTree == true:
            time.sleep(1)
        break
    print("The letter that you chose was " + letters)
    if letters == "r":
        print("Nice! You guessed one letter from the word!\n t r _ _")
        
    if letters == "e":
        print("Wow! You guessed two letters from the word!\n t _ e e")
    if letters == correct:
        print("Correct! The word was tree!")
    if letters == lettertree:
        print("Sorry, that's not a correct letter, feel free to try again.")
    limbCount -=1
    if limbCount == 0: 
        print("Unfortunately, you are out of tries, better luck next time!")
        time.sleep(1)
        exit()
        

Sorry if my code seems sloppy, I'm just starting to learn Python after learning other programming languages.


Solution

  • Here's how I would construct the string you're looking for. When a player guesses a letter, I'd store it in a set, called guesses. If it's in the set of letters in the word, called correct, then I effectively add it to the intersection of these sets, called correct_guesses. Then, I iterate over the word, and print out each letter into the placeholder string, replaced with an underscore if that letter is not in correct_guesses. This is what it would look like in code:

    word = 'tree'
    guesses = set()
    correct = set(list(word))
    correct_guesses = set()
    
    while correct_guesses != correct:
      # Get input
      guess = input("Guess: ")
      guesses.add(guess)
      correct_guesses = guesses.intersection(correct)
    
      # Print word status
      placeholder = ' '.join(['%s' for _ in word])
      print(placeholder % tuple([l if l in correct_guesses else '-' for l in word]))
      print("You've guessed: " + ", ".join(guesses))
    

    Let me know if this code doesn't work for you or if it needs more explaining.