secret_word = "python"
correct_word = "yo"
count = 0
for i in secret_word:
if i in correct_word:
print(i,end=" ")
else:
print('_',end=" ")
使代碼的結果會是這樣_ y _ _ o _
我的問題是我怎麼能我使用while循環,而不是使用For循環得到的結果相同。我知道我必須使用索引來遍歷每個字符,但是當我嘗試失敗時。有什麼幫助嗎?使用while循環,而不是爲循環
while count < len(secret_word):
if correct_word [count]in secret_word[count]:
print(correct_word,end=" ")
else:
print("_",end=" ")
count = count + 1
感謝
讓我們看看您嘗試失敗的代碼。 – Kevin
你'count'沒有正確縮進。 'secret_word'中不需要'count'只是使用:'如果secret_word中的correct_word [count]:' –