how to avoid recurring displayed?

69 views Asked by At

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

1

There are 1 answers

0
NoDakker On

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.

import random
import time                         # Provide functionality for sleep/delay
from os import system, name         # Provide functionality to clear the screen

def clx():                          # Generic screen clear

    if name == 'nt':                # Windows screen clear
        system('cls')
 
    else:
        system('clear')             # Linux/Posix clear

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

clx()

while repeater:
    
    print(f"Word: {' '.join(displaylist)}")
    print(f"found letters: {' '.join(found_letters)}")
    print(f"missed letters: {' '.join(missed_letters)}")
    print("\n\n")

    letter = input("Enter a letter:")

    if letter in word:

        if letter in displaylist:
            print("You have already entered that letter")
            time.sleep(2)
            clx()
            continue

        for i in range(len(word)):      

            if letter == word[i]:
                displaylist[i] = letter
                found_letters.append(letter)

        if '_' not in displaylist:              # Check to see if the word has been guessed
            clx()
            print(f"Word: {' '.join(displaylist)}")
            print(f"found letters: {' '.join(found_letters)}")
            print(f"missed letters: {' '.join(missed_letters)}")
            print("\n\n")
            print("You have guessed the word")
            print(f"Word: {''.join(displaylist)}")
            break

        else:
            clx()
            continue

    elif letter.isalpha():

        if letter in missed_letters:
            print("You have already tried that letter and it is not in the word")
            time.sleep(2)
            clx()
            continue

        missed_letters.append(letter)
        clx()
        continue

    else:
        print("you have to enter a letter:")
        repeater = False

Here are the key points.

  • The "os" and "time" packages are imported to provide operating specific screen clearing functionality along with a time delay as noted in your expected output sample.
  • Testing is made for the actual operating system so that the proper screen clearing command is evoked as referenced in the following link "https://www.geeksforgeeks.org/clear-screen-python/".
  • Some additional testing was added such that if all of the letters of the word have been guessed, that is outputted and the program ends.

Testing this out at the terminal appeared to mimic the functionality you were after.

Word: o r a n g e
found letters: a o r n g e
missed letters: h j k l w q



You have guessed the word
Word: orange

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.