Python:比较相同值的两个字符串(猜测应用程序)

发布于 2025-02-09 00:30:46 字数 941 浏览 2 评论 0原文

我正在尝试创建一个单词猜测程序,并且它具有一个类似“快乐”的单词,让我们说“ 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 技术交流群。

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

发布评论

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

评论(2

独守阴晴ぅ圆缺 2025-02-16 00:30:46

使用集合并检查字母是否在其中...沿着

target = "Happy"
guesses = ["Nothi", "Heapy", "Happy"]
 
lst = []
letters = set(target)


for guess in guesses:
    print (guess,": ", *["X" if a==b else                # identical
                         "O" if b in letters else        # somewhere in word
                         "_" for a,b                     # not in word
                         in zip(target, guess)], sep="") # for all letter-tuples

将打印

Nothi: _____
Heapy: X_OXX
Happy: XXXXX

的东西而不是按固定数字索引,我将两个单词(相同的长度)划在一起,以便您从

"aaaa"
"1234"

元组

("a","1"), ("b","2"), ("c","3"), ("d","4")

中获得比较。

Use a set and check if the letter is in it ... something along the lines of

target = "Happy"
guesses = ["Nothi", "Heapy", "Happy"]
 
lst = []
letters = set(target)


for guess in guesses:
    print (guess,": ", *["X" if a==b else                # identical
                         "O" if b in letters else        # somewhere in word
                         "_" for a,b                     # not in word
                         in zip(target, guess)], sep="") # for all letter-tuples

will print

Nothi: _____
Heapy: X_OXX
Happy: XXXXX

Instead of indexing by a fixed number, I zip together the two words (same lengths) so you get from

"aaaa"
"1234"

tuples

("a","1"), ("b","2"), ("c","3"), ("d","4")

to compare them.

初懵 2025-02-16 00:30:46

您快到了,您只需要一个elif条件就可以检查字母的存在:

def evaluate(target, guess):
    result = list()
    for i, c in enumerate(guess):
        if c == target[i]:
            result.append('X')
        elif c in target:
            result.append('O')
        else:
            result.append('_')
    return ''.join(result)


print(evaluate('happy', 'heapy'))

You're almost there, you just need an elif condition to check for existence of the letter:

def evaluate(target, guess):
    result = list()
    for i, c in enumerate(guess):
        if c == target[i]:
            result.append('X')
        elif c in target:
            result.append('O')
        else:
            result.append('_')
    return ''.join(result)


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