-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