我正在做一些练习,想将“ macdonald”更改为“ MacDonald”。但是,我的输出仅显示最后一个值为“ True”的值,即“ D”。如何让程序显示全文?
name = 'macdonald'
def myfunc(name):
for x in name:
finalName = ""
if x == name[0] or name[3]:
finalName += x.capitalize()
else:
finalName += x
return finalName
答案 0 :(得分:2)
name = 'macdonald'
def myfunc(name):
finalName = ""
for i, x in enumerate(name):
if i == 0 or i == 3:
finalName += x.capitalize()
else:
finalName += x
return finalName
print(myfunc(name))