Secret Word Game - Python

83 views Asked by At

I'm starting to study programming now and I have to do a secret word game, but whenever I run the code the result is concatenated and it was supposed to appear only once, where am I going wrong? I'll paste the code and attach a screenshot of the console. I'm open to other tips and improvements.

print('¨' * 40)
print(f'|{"Secret Word Game":^38}|')
print('¨' * 40)

secret_word = 'believer'
user_input = ''
typed = []
attempts= 0
number_of_letters = '_ ' * len(secret_word)
discovering_secret_word = ''


print('Try to guess the Secret Word!')
print(f'The secret word is: {number_of_letters}')

while discovering_secret_word != secret_word:
    
    user_input = input('Type a letter: ')
    attempts += 1

    if len(user_input) > 1:
        print('You can´t insert more than one letter!')
        continue

    typed.append(user_input)

    for letter in secret_word:
        if letter in typed:
            discovering_secret_word += letra
        else:
            discovering_secret_word += '_ '
    print(f'The Secret Word is {discovering_secret_word}')
else: 
    print('Congratulations, you won!')
    print(f'The Secret Word is {secret_word}.')
    print(f'It took {attempts} attempts.')
`

Console

1

There are 1 answers

0
Marcelo Paco On BEST ANSWER

The issue occurs because your are not resetting the discovering_secret_word string. You are constantly concatenating into it. Here is what you need to do:

print('¨' * 40)
print(f'|{"Secret Word Game":^38}|')
print('¨' * 40)

secret_word = 'believer'
user_input = ''
typed = []
attempts = 0
number_of_letters = '_ ' * len(secret_word)
discovering_secret_word = ''


print('Try to guess the Secret Word!')
print(f'The secret word is: {number_of_letters}')

while discovering_secret_word != secret_word:

    user_input = input('Type a letter: ')
    attempts += 1

    if len(user_input) > 1:
        print('You can´t insert more than one letter!')
        continue

    typed.append(user_input)

    # reset discovering_secret_word
    discovering_secret_word = ""
    for letter in secret_word:
        if letter in typed:
            discovering_secret_word += letter # Fixed to letter had letra before
        else:
            discovering_secret_word += '_ '
    print(f'The Secret Word is {discovering_secret_word}')
else:
    print('Congratulations, you won!')
    print(f'The Secret Word is {secret_word}.')
    print(f'It took {attempts} attempts.')

This is what my console looked like after I attempted to solve for the secret word:

¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨
|           Secret Word Game           |
¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨
Try to guess the Secret Word!
The secret word is: _ _ _ _ _ _ _ _
Type a letter: b
The Secret Word is b_ _ _ _ _ _ _
Type a letter: b
The Secret Word is b_ _ _ _ _ _ _
Type a letter: e
The Secret Word is be_ _ e_ e_
Type a letter: l
The Secret Word is bel_ e_ e_
Type a letter: i
The Secret Word is belie_ e_
Type a letter: v
The Secret Word is believe_
Type a letter: r
The Secret Word is believer
Congratulations, you won!
The Secret Word is believer.
It took 7 attempts.