简体   繁体   English

遍历字符串以找到匹配的字符并生成不同的表情符号

[英]Iterate through a string to find matching characters and produce different emoji

How do I call contains_char from my emojified function?如何从表情符号化的emojified中调用contains_char

I want to be able to iterate through the guess_word to see if there are any matching letters in the secret_word .我希望能够遍历guess_word以查看secret_word中是否有任何匹配的字母。 If there are matching letters, the output should have green emoji boxes.如果有匹配的字母,output 应该有绿色的表情符号框。 If there are no matching letters, the output should have white emoji boxes.如果没有匹配的字母,output 应该有白色的表情符号框。 If there are letters present but they are not in the right position, the output should have yellow boxes.如果存在字母但不在正确的 position 中,则 output 应该有黄色框。

def contains_char(any_length: str, single_character: str) -> bool:
    """Loop iterates through each character in string to find matching character."""
    assert len(single_character) == 1
    if single_character in any_length:
        return True 
    else:
        return False

def emojified(guess_word: str, secret_word: str) -> str:
    """A way to match letters to its corresponding emoji color output. """
    assert len(guess_word) == len(secret_word)
    WHITE_BOX: str = "\U00002B1C"
    GREEN_BOX: str = "\U0001F7E9"
    YELLOW_BOX: str = "\U0001F7E8"
    emoji_color: str = ""
    i: int = 0 
    contains_char
    while i < len(guess_word):
        if guess_word[0] == secret_word[0]:
            emoji_color += GREEN_BOX
        else:
            emoji_color += WHITE_BOX
            
        print(emoji_color)

I want the output to look like this picture below.我希望 output 如下图所示。 Thank you!谢谢!

图片

You are vastly overcomplicating this.你把这个复杂化了。

def emojified(guess_word: str, secret_word: str) -> str:
    assert len(guess_word) == len(secret_word)
    WHITE_BOX: str = "\u2B1C"
    GREEN_BOX: str = "\U0001F7E9"
    YELLOW_BOX: str = "\U0001F7E8"
    output: list = []
    this_box: str = ''
    for i, char in enumerate(guess_word):
        if i < len(secret_word) and secret_word[i] == char:
            this_box = GREEN_BOX
        elif char in secret_word:
            this_box = YELLOW_BOX
        else:
            this_box = WHITE_BOX
        output.append(this_box)
    return ''.join(output)

The assert is perhaps not the best way to handle this; assert可能不是处理这个问题的最佳方式; Python can disable assert statements in some situations. Python 可以在某些情况下禁用断言语句。 If the check should be part of your production logic, probably refactor it to raise an exception instead.如果检查应该是您的生产逻辑的一部分,则可能会重构它以引发异常。

The condition i < len(secret_word) is redundant if you make sure both strings are the same length, but I thought it safer to leave it in in case you want to relax that condition.如果您确保两个字符串的长度相同,则条件i < len(secret_word)是多余的,但我认为保留它更安全,以防您想放宽该条件。

Appending to strings is relatively expensive, so a good practice is to collect things into a list and only join the list into a string at the end.追加到字符串的成本相对较高,因此一个好的做法是将事物收集到一个列表中,并且只在最后将列表加入到一个字符串中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM