0

I want to print a message on the screen saying "you have already entered that letter" ONLY in the case the letter was entered previously. If the letter is being entered for the first time it should not print that. Below is my code but it prints the message even when the letter is entered for the first time:

import requests
import random
import hangman_art #import logo
word_site = "https://www.mit.edu/~ecprice/wordlist.10000"
response = requests.get(word_site)
WORDS = response.text.splitlines() 
chosen=random.choice(WORDS) #use this or lines 8,9,10
#https://stackoverflow.com/questions/75139406/how-to-pick-a-string-from-a-list-and-convert-it-to-a-list-python?noredirect=1#comment132596447_75139406
# ~ rand=random.randint(0,10000)
# ~ pick=WORDS[rand]
# ~ pick_as_list=list(pick)
print(hangman_art.logo)
print(chosen)
stages = ['''
  +---+
  |   |
  O   |
 /|\  |
 / \  |
      |
=========
''', '''
  +---+
  |   |
  O   |
 /|\  |
 /    |
      |
=========
''', '''
  +---+
  |   |
  O   |
 /|\  |
      |
      |
=========
''', '''
  +---+
  |   |
  O   |
 /|   |
      |
      |
=========''', '''
  +---+
  |   |
  O   |
  |   |
      |
      |
=========
''', '''
  +---+
  |   |
  O   |
      |
      |
      |
=========
''', '''
  +---+
  |   |
      |
      |
      |
      |
=========
''']

x=[]
lives=6
for letter in chosen:
    x+='_'
s=' '.join(x)    
print(s)
while '_' in x:
    user=input("Guess a letter: ").lower()
    if user in chosen:                       #<---------------------------- My trial
        print(f"You've already guessed {user}")
    for i in range(0,len(chosen)):
        if chosen[i]==user:
            x[i]=user
    s=' '.join(x)
    print(s)
    if '_' not in x:
        print("You Win!")
    if user not in chosen:
        lives-=1
        print(stages[lives])
        print(f"You guessed {user}, that's not in the word. You lose a life ({lives} left).")
        if lives==0:
            print("You lose.")
            break   

Also I noticed it takes a life from the user if they repeat the wrong letter. I want it to take a life only for the first trial of a wrong letter.

Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
  • The [tag:pygame] tag is only for questions about the [Pygame](https://www.pygame.org/news) framework, but not for questions about games written with Python. – Rabbid76 Jan 17 '23 at 21:12
  • 1
    `chosen` is not the list of letters that the user has tried before. So `if user in chosen:` is not the correct condition. – Barmar Jan 17 '23 at 21:13
  • What is `chosen` supposed to represent? It looks like you initialize it to `random.choice(WORDS)`, but if it's supposed to represent the set of letters guessed by the user, shouldn't it start off as empty? – Samwise Jan 17 '23 at 21:14
  • 1
    You don't have a list of all the user's choices. You need a list like `previous_choces = []` and then do `previous_choices.append(user)` after each choice. – Barmar Jan 17 '23 at 21:15
  • [How to debug small programs.](//ericlippert.com/2014/03/05/how-to-debug-small-programs/) | [What is a debugger and how can it help me diagnose problems?](//stackoverflow.com/q/25385173/843953) – Pranav Hosangadi Jan 17 '23 at 22:10

1 Answers1

-3

Simplest thing to do is add a "continue" right after the "You've already guessed" print statement. However, I would also advise some better programming practices and use elif and else statements.

Michael Cao
  • 2,278
  • 1
  • 1
  • 13
  • 2
    This should be a comment, not an answer. It's not even a good comment - "*I would also advise some better programming practices*" is not helpful at all, nor is the rest of the sentence. – MattDMo Jan 17 '23 at 21:25