我对循环的处理有疑问。这是适用于Python的子手的代码。尽管对每个角色使用列表或仅打印似乎是执行任务的更简便方法,但我想知道,为了避免将来遇到的麻烦,提出的解决方案的问题在哪里。
word='heksakosjokontaheksafobia'
def hangman(input, word_to_guess):
users_guess=input('What is the first letter of your first thought?\n')
print (users_guess)
if users_guess in word_to_guess:
unknown_chararcters='*'*len(word_to_guess) # The creation of a string showing number of uknown characters
print(unknown_characters)
for char in word_to_guess:
if char==users_guess: # Checking whether input is included in word_to_guess
ind=word_to_guess.index(char)
unknown_characters=unknown_characters[:ind]+users_guess+unknown_characters[ind+1:] # Substitution of '*' with input
continue
print(unknown_characters)
hangman(input, word)
如您所见,单词包含许多'a',但是对于input == a,输出为:
What is the first letter of your first thought?
a
*************************
****a********************
如何强制循环遍历字符串的以下字符?