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.