Python:比较相同值的两个字符串(猜测应用程序)
我正在尝试创建一个单词猜测程序,并且它具有一个类似“快乐”的单词,让我们说“ heapy”,并且它在h,p,y中为x,含义这些值是正确的(基本上是wordle),而o则是他们在词中,但不是正确的地方。
我设法创建了它们是否处于确切位置,并指出不是那些的空间“ a”然后将其添加到列表中,以便它将打印x,_ o,x,x x是正确的位置 _这封信根本不存在 哦,它在那里,但位于错误的地方。
我的这一部分的代码如下:
target = "Happy"
guess = "Heapy"
# Part 1 of 3
lst = []
name = ""
targetCount = 0
while targetCount < 5:
# print(guess[counter])
if guess[targetCount] == target[targetCount]:
print("X")
lst.append(guess[targetCount])
else:
print(".")
lst.append(".")
targetCount += 1
print("---------------------------------")
for x in lst:
print(x)
print("Next: ---------------------------------")
# Part 2 of 3
targetCount = 0
yCount = 0
List = 0
guess_index = []
# index of the two '.' in word
for List in range(len(lst)):
if lst[List] == ".":
guess_index.append(List)
print(List)
我觉得我在这里很愚蠢,但是如果你们可以帮助我理解,这真是太神奇了。
I am trying to create a word guessing sort of program and it has a word like 'happy' you enter lets say 'heapy' and it takes in H, P, Y as X's meaning those values were correct (basically wordle) and O's if they are in the word but not the right place.
I have managed to create if they are in the exact position or not and point out the space between the ones that aren't but I am having trouble figuring out how to compare the two strings to see if it contains any letters the same, example the 'a' and then to add it to the list so it would print X, _ O, X, X
X being that it is in the correct place
_ being that the letter isnt there at all
and O being it is in there but in the wrong place.
My code for this part is as follows:
target = "Happy"
guess = "Heapy"
# Part 1 of 3
lst = []
name = ""
targetCount = 0
while targetCount < 5:
# print(guess[counter])
if guess[targetCount] == target[targetCount]:
print("X")
lst.append(guess[targetCount])
else:
print(".")
lst.append(".")
targetCount += 1
print("---------------------------------")
for x in lst:
print(x)
print("Next: ---------------------------------")
# Part 2 of 3
targetCount = 0
yCount = 0
List = 0
guess_index = []
# index of the two '.' in word
for List in range(len(lst)):
if lst[List] == ".":
guess_index.append(List)
print(List)
I feel like I'm being silly here but if you guys could please help me understand that would be amazing <3
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用集合并检查字母是否在其中...沿着
将打印
的东西而不是按固定数字索引,我将两个单词(相同的长度)划在一起,以便您从
元组
中获得比较。
Use a set and check if the letter is in it ... something along the lines of
will print
Instead of indexing by a fixed number, I zip together the two words (same lengths) so you get from
tuples
to compare them.
您快到了,您只需要一个
elif
条件就可以检查字母的存在:You're almost there, you just need an
elif
condition to check for existence of the letter: