0

I'm trying to print a string that includes letters in the alphabet. After each letter the user guesses, that guessed letter would be removed from the alphabet and then printed in the console to show which letters have not been guessed yet.

So far I have tried remove(), translate(), and filter(). Every time I've tried, the letter is successfully removed but when it's time to remove another letter, the first letter reappears in the string and the second letter is now removed. Basically, the letters do not stay removed and are only gone for that one loop.

Background info: I'm making a hangman app in python3. So part of the code I'm having trouble with is within a while loop that is in a function. I'm making a hangman app in python3. So part of the code I'm having trouble with is within a while loop that is in a function.

alphabet = "abcdefghijklmnopqrstuvwxyz"
guess = " "

print("These letters haven't been guessed yet: " + alphabet.translate({ord(guess): None}))

Expected result:

(Notice that the previous do not reappear after the second input.)

guess = input("Enter a letter: ")
Enter letter: a
These letters haven't been guessed yet: bcdefghi

guess = input("Enter a letter: ")
Enter letter: h
These letters haven't been guessed yet: bcdefgi

Actual result: (Notice that the letter 'a' has reappeared after user inputs 'h' .)

guess = input("Enter a letter: ")
Enter letter: a
These letters haven't been guessed yet: bcdefghi

guess = input("Enter a letter: ")
Enter letter: h
These letters haven't been guessed yet: abcdefgi
4
  • list of 26 characters of holding value of type boolean. Every time you guess the character, turn them to True which means it can't be used. As simple. Commented Sep 8, 2019 at 9:46
  • 2
    You aren't reassigning the value of alphabet, only printing a copy of it without a letter, so of course the letter will still be there in the actual variable itself. Try putting alphabet = alphabet.translate(...). Commented Sep 8, 2019 at 9:51
  • translate returns a modified copy it does not modify alphabet itself. you need to reassign it. Commented Sep 8, 2019 at 9:54
  • Interesting idea Commented Apr 30 at 1:06

3 Answers 3

2

You can use sets for this. First you create a set containing all the letters from the alphabet. Also, you want to track which are the guessed words in another set. Then, each time a user guess some letter, you add it to the guessed set. At the end, you can just print the alphabet - guessed. At the end, I just used ''.join for better output format.

alphabet = set("abcdefghijklmnopqrstuvwxyz")
guessed = set()

guess = "a"
guessed.add(guess)

print("These letters haven't been guessed yet: " + str(alphabet - guessed))
#Output: These letters haven't been guessed yet: {'c', 'd', 'y', 'r', 'i', 'h', 'g', 'o', 'q', 'x', 'v', 'k', 'j', 'z', 'p', 'n', 'u', 'm', 'l', 'b', 't', 'w', 'e', 'f', 's'}

print("These letters haven't been guessed yet: " + ''.join(alphabet - guessed))
#Output: These letters haven't been guessed yet: cdyrihgoqxvkjzpnumlbtwefs

Update from @MrFuppes: use the function sorted to print the elements from the sets in alphabetical order

print("These letters haven't been guessed yet: " + ''.join(sorted(alphabet - guessed)))
#Output: These letters haven't been guessed yet: bcdefghijklmnopqrstuvwxyz
Sign up to request clarification or add additional context in comments.

3 Comments

I'd add sorted for readability and make it an f-string: print(f"These letters haven't been guessed yet: {' '.join(sorted(alphabet - guessed))}") ;-)
@MrFuppes, I did not pay attention to this detail, thank you. I'll also add your modification in my answer.
thanks for the help everyone! Exactly what I needed, totally blanked on the fact that strings are immutable in python
1

You could also use the str.replace method (https://docs.python.org/3.6/library/stdtypes.html#str.replace), but like above, you have to reassign the value of alphabet.

alphabet = alphabet.replace('a', '')

Comments

1

You are having a problem because strings in python are immutable in nature. It means you cannot change them. Remember using update etc basically changes the address to which your variable is referencing. So I think you probably forgot to update alphabet variable in your code.

HERE IS WHAT YOU CAN DO -

alphabet = "abcdefghijklmnopqrstuvwxyz"
guess = input("Enter a letter: ")
  CONSOLE - Enter a letter: a
alphabet = alphabet.replace(guess, '')
print('These letters haven\'t been guessed yet:', alphabet)
  CONSOLE - These letters haven't been guessed yet: bcdefghijklmnopqrstuvwxyz
guess = input("Enter a letter: ")
  CONSOLE - Enter a letter: h
alphabet = alphabet.replace(guess, '')
print('These letters haven\'t been guessed yet:', alphabet)
  CONSOLE - These letters haven't been guessed yet: bcdefgijklmnopqrstuvwxyz

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.