Move the increment out of the if clause, otherwise it will remain inside the while loop:

def capitalise():
    x = input("enter the word= ")

    a = 0

    while a < len(x):
        if x[a].islower():
            x = x[:a] + x[a].upper() + x[a + 1:]
        a = a + 2 # this line changed
    print(x)

A more efficient way (and a bit more pythonic) is to use iterate over the characters using enumerate as follows:

def capitalise():
    x = input("enter the word= ")
    characters = list(x)
    result = "".join([c.upper() if i % 2 == 0 else c for i, c in enumerate(characters)])
    print(result)

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top