Я пытаюсь сделать игру на угадывание слов в Python, но последняя часть меня немного смущает.
Вот мой код до сих пор:
word_tuple = ("c", "o", "d", "e", "c")
word = ""
word = input("Give a word of " + str(len(word_tuple)) + " characters: ")
while len(word) != len(word_tuple):
if len(word) != len(word_tuple):
print("Wrong!")
word = input("Give a word of " + str(len(word_tuple)) + " characters: ")
for i in range(len(word_tuple)):
print(word_tuple[i], end="")
По сути, цикл проверяет, вставляете ли вы 5-символьное слово, и, если вы это делаете, сравнивает слово с символами кортежа. Если 1 или более символов верны, он напечатает правильные символы, а те, которые не были угаданы, замаскированы символом, например '*'.
Запутанная часть - где я должен проверить, есть ли во введенном слове символы, соответствующие кортежу, и затем распечатать правильные символы.
Так, например:
Give a word of 5 characters: Python
Wrong!
Give a word of 5 characters: Candy
Almost there! The word is "C*d*c"
Give a word of 5 characters: Denim
Almost there! The word is "C*dec"
Give a word of 5 characters: Codec
You found the word!
Любая помощь будет принята с благодарностью.
Ваша проблема в том, что вы не печатаете свое слово правильно, и ваш отпечаток устарел, вот ответ, который вы можете попробовать
word_tuple = ("c", "o", "d", "e", "c")
# We use this list to keep in memory the letters found
found = [False] * len(word_tuple)
word = ""
# The 'all' method return True only if the list contains only True values
# Which means, while all letters are not found
while not all(found):
# the 'lower' method allows you to not take in account the uppercases
word = input("Give a word of " + str(len(word_tuple)) + " characters: ").lower()
if len(word) == len(word_tuple):
for charac in word_tuple:
if charac in word:
found = [b or word_tuple[index] in word for index, b in enumerate(found)]
# The 'any' method return True only if the list contains at least one True value
# Which means we print Wrong only if there is no letter found
if not any(found):
print('Wrong!')
else:
print('Almost there! The word is "', end='')
for i in range(len(word_tuple)):
if found[i]:
print(word_tuple[i], end="")
else:
print('*', end='')
print('"')
else:
print('Wrong!')
# The method 'join' allows you to join every string of an iterable
# Which means it joins every character of your tuple to a printable string
while word != ''.join(word_tuple):
print('Close, try again')
word = input("Give a word of " + str(len(word_tuple)) + " characters: ").lower()
print('You found the word!')
Упражнение может быть рефакторингом этого кода различными способами.