将 while 循环输入附加到字符串?
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许这可以直接使用
set
来实现,删除剩余的字符并中间显示它们Possibly this can be implemented directly with a
set
, removing remaining characters and intermediately displaying them