-1
所以我剛剛學會了如何操作代碼學院的for循環中的單個字母。 但讓我們說我做了一個函數,並希望這個函數來操縱用戶輸入的單詞的元音,並用四個連續的副本替換元音。我會怎麼做呢?for循環中的字符串操作
預期輸出:
>>>Exclamation("car")
caaaar
>>>Exclamation("hello")
heeeelloooo
到目前爲止,我有:
word = input("Enter a word: ")
vowels= ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']
for char in word:
if char in vowels:
print(____,end='') #here I am unsure of how to replace it with consecutive copies of itself
else:
print(char,end='')
但是,您的代碼僅包含_printing_輸出(一種完全有效的實驗方式)。你是否考慮編寫一個函數來進行替換?通過這種方式,您可以將打印從替換邏輯中分離出來,從而保持代碼更清晰。 – holdenweb