将 while 循环输入附加到字符串?

发布于 2025-01-17 07:55:34 字数 713 浏览 0 评论 0原文

我正在使用 Python 3,目前正在尝试创建基于文本的 Wordle 版本。除了字母表功能之外,我已经记下了大部分代码。

本质上,它是一个字母表,可以消除您得到的 6 个猜测中根本不属于正确答案的字母。事实上,它类似于 Hangman 游戏。

现在,为了尝试在游戏中实现这一点,我有一段代码来检查用户对答案中没有的字母的猜测,并在删除答案中任何位置的字母时保留它们。

问题是我想将这些丢弃的字母附加到外部字符串中,该字符串被输入到一个系统中,该系统从字符串 alphabet 中删除所述字母(“ABCDEFGHIJKLMNOPQRSTUVWXYZ)。不幸的是,我正在使用 while 循环来获取输入,但它似乎不起作用

总之,我想找到一种方法来从 while 循环中获取字符串输入并将其附加到外部字符串,我想这是。看起来就像代码中这样:

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)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

微暖i 2025-01-24 07:55:34

也许这可以直接使用set来实现,删除剩余的字符并中间显示它们

>>> 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
>>>

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
>>>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文