如何为变量分配不同的变体,以便如果输入与分配的变量之一匹配,则答案是正确的

how do I assign different variation to a variable so if input matches one of assigned variable then the answer is correct

创建一个猜词游戏,secret_word 可以是任何变体,但我如何编写 secret_word 的不同变体才能被程序识别?

在这种情况下,密码是 "Korea",我如何才能统一任何变体,或者我是否必须插入每一种不同的变体?

secret_word = {"korea", "kOrea", "KoRea", "KoReA", "KOrea, "KORea", 
"Korea", "KOREA"}
guess = ""
guess_count = 0
guess_limit = 3
out_of_guesses = False

while guess != secret_word and not (out_of_guesses):
    if guess_count < guess_limit:
        guess = input("Guess a word: ")
        guess_count += 1
    else:
        out_of_guesses = True

if out_of_guesses:
print("Maybe Next time! You are out of guesses")
else:
    print("You win!")

我会用小写(或大写)来保密,然后将猜测转换为小写:

secret_word = 'korea'

while guess.lower() != secret_word and not (out_of_guesses):
    # loop's body...

简而言之:不区分大小写的检查是一个比乍看起来更难的问题。 str.casefold() function [python-doc] 应该为此类比较生成一个字符串。

你检查输入字符串的 .casefold() 是否与要猜测的字符串的 .casefold() 相同,如:

secret_word = 'korea'
guess_count = 0
guess_limit = 3

while guess_count < guess_limit:
    guess = input('Guess a word')
    if <b>guess.casefold() == secret_word.casefold()</b>:
        break
    else:
        guess_count += 1

if guess_count < guess_limit:
    print('You win')
else:
    print('You lose')

.casefold() 应该是 Unicode standard to produce a string that can be compared for case-insensitive comparisons. For example in German, the eszett ß [wiki] 大写映射到:

>>> 'ß'.lower()
'ß'
>>> 'ß'.upper()
'SS'
>>> 'SS'.lower()
'ss'
>>> 'ß'.lower() == 'SS'.lower()
False

.casefold() 将 return ss:

>>> 'ß'.casefold()
'ss'
>>> 'ss'.casefold()
'ss'
>>> 'ß'.casefold() == 'SS'.casefold()
True

case-insensitive 比较结果是一个难题,因为某些字符没有 upper/lowercase 等价物,等等

您可以使用 guess.lower()guess 的每个变体转换为小写。 所以你只需要给出小写的变化。

lower(self, /) Return a copy of the string converted to lowercase.
-- help(str.lower)

secret_word = "korea"
guess = ""
guess_count = 0
guess_limit = 3
out_of_guesses = False

while guess.lower() != secret_word and not (out_of_guesses):
    if guess_count < guess_limit:
        guess = input("Guess a word: ")
        guess_count += 1
    else:
        out_of_guesses = True

if out_of_guesses:
    print("Maybe Next time! You are out of guesses")
else:
    print("You win!")