I want to create a program that take a word randomly from a list and ask the user to enter a letter, if the letter is in the word it's added or displayed directly to a line called found letters, if it's not in the word it's added to a line called missed letter, and if the user input a letter that already has been inputted a message displayed and tell him that he is already input the letter and invite him to try a new letter, and if he enters anything beside a letter the program stops.
import random
words = ['cat', 'why', 'car', 'dress', 'apple', 'orange']
word = random.choice(words)
displaylist = []
found_letters = []
missed_letters = []
for _ in range(len(word)):
displaylist.append('_')
repeater = True
while repeater:
print(f"Word: {' '.join(displaylist)}")
print(f"found letters: {' '.join(found_letters)}")
print(f"missed letters: {' '.join(missed_letters)}")
letter = input("Enter a letter:")
if letter in word:
if letter in displaylist:
print("you have already entered that letter:")
continue
for i in range(len(word)):
if letter == word[i]:
displaylist[i] = letter
found_letters.append(letter)
continue
elif letter.isalpha():
missed_letters.append(letter)
continue
else:
print("you have to enter a letter:")
repeater = False
the problem is that each time the user input a letter, it's displayed to a new-found letters line or missed letters line, but I want them to be fixed the only thing change is the letters, when the user input some letters ,and they are in the word, they are being added directly to one found letters line and to a missed letters line if they are not in the word , not to repeat found letters and missed letters lines each time. Does any one has an idea how can I do this
I observed your desired output, and taking in account the good comment about "ncurses", if for some reason you don't want to go that route, an alternate option would be to utilize some operating system functions to clear the screen. With that in mind, following is a refactored version of your code with a simplified screen clearing functionality. My apologies for its length, but the enhancements were spread throughout your program.
Here are the key points.
Testing this out at the terminal appeared to mimic the functionality you were after.
The main takeaway from this possible route/solution (and this probably is just one of many solutions) is that you may want to delve some more into Python tutorials, especially as it pertains to function usage, while loop usage with "continue" and "break" statements.