Home > database >  Appending while loop inputs to a string?
Appending while loop inputs to a string?

Time:03-26

I'm using Python 3, and I'm currently attempting to create a text-based version of Wordle. I've got much of the code down except for its alphabet feature.

In essence, it's an alphabet that eliminates any letter in the 6 guesses you get that isn't in the correct answer at all. It's similar to the game Hangman, in fact.

Now, to try and implement this into the game, I have a piece of code that checks the user's guess for any letters not in the answer, and leaves them while deleting letters that are in any place in the answer.

The problem is that I want to append these discarded letters to an outside string, which gets fed into a system that removes said letters from the string alphabet ("ABCDEFGHIJKLMNOPQRSTUVWXYZ). Unfortunately, I am using a while loop to obtain inputs, and it doesn't seem to work.

In summary, I want to find a way to have string inputs be obtained from within a while loop and appended to an outside string, which I imagine looks like this in code:

s = ""
while True:
    # If the input is not "END", it gets added to variable *s* until the user types EN
    foo = str(input("Enter word to append to string; type END to end loop: "))
    if foo == "END":
         break
    else:
         s  = foo
print(s)

CodePudding user response:

Possibly this can be implemented directly with a set, removing remaining characters and intermediately displaying them

>>> from string import ascii_uppercase as AZ_UPPER
>>> letters = set(AZ_UPPER)
>>> while letters:
...     ipt = input("".join(sorted(letters))   ": ")
...     letters -= set(ipt.upper())
...
ABCDEFGHIJKLMNOPQRSTUVWXYZ: The quick, brown fox jumps over the normal dog
YZ: lazy
>>>
  • Related