-1

I am doing some practice and want to change 'macdonald' to 'MacDonald' However, my output is just displaying the last value that is 'True', which is 'D'. How do I have the program display the full text?

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
InstantNut
  • 93
  • 1
  • 9
  • 2
    You are making a new `finalName = ""` for every iteration of the loop. Maybe move it outside the `for` loop? You might be better off just slicing: `name[0:3].capitalize() + name[3:].capitalize()` – Mark Jun 10 '19 at 20:24
  • 1
    Notice that for a name like MacDermot, you will get MacDerMot even with correct code. Look into `enumerate` instead – Mad Physicist Jun 10 '19 at 20:30

1 Answers1

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))
abdullah.cu
  • 674
  • 6
  • 11