-2

I want to do a program where I have this input: New Word and this output: NeW wOrD

def changeCase():
i=0
while i <= len(word):
    if((i%2)==0):
    print(word[i].upper())
else:
    print(word[i].lower()
    i=i+1

3 Answers3

2

Your function needs to take the word as a parameter, and the indentation inside the function needs to be corrected.

def changeCase():  # needs "word" parameter
i=0  # needs to be indented to be inside function
while i <= len(word):
    if((i%2)==0):
    print(word[i].upper())  # needs to be indented to be inside if:
else:  # needs to be indented to match the if:
    print(word[i].lower()  # missing a closing )
    i=i+1  # needs to be un-indented to be outside else:

With those fixes we have:

def changeCase(word):
    i=0
    while i <= len(word):
        if((i%2)==0):
            print(word[i].upper())
        else:
            print(word[i].lower())
        i=i+1

changeCase("New Word")

That gets us:

N
e
W

W
o
R
d
Traceback (most recent call last):
  File "C:\test\python\test.py", line 10, in <module>
    changeCase("New Word")
  File "C:\test\python\test.py", line 5, in changeCase
    print(word[i].upper())
IndexError: string index out of range

This is because the valid range of indices in a sequence is one less than the len (because the indices start at zero).

Changing while i <= len(word) to while i < len(word) fixes the IndexError, but we still are printing each letter on one line. We can fix that by adding end='' to our print calls, and adding a final print() at the end to add the finishing newline:

def changeCase(word):
    i=0
    while i < len(word):
        if((i%2)==0):
            print(word[i].upper(), end='')
        else:
            print(word[i].lower(), end='')
        i=i+1
    print()

changeCase("New Word")

gives us:

NeW WoRd

But we still have the problem here that we're alternating the case on every character, and based on the example it seems like we only want to alternate on every letter. So we don't want i to be the index of the character in the string, we want it to be the current count of alphabetical characters. To handle that it's probably easiest if we change our while loop to a for loop over the characters in the string, and only increment i if the character is alphabetical:

def changeCase(word):
    i = 0
    for c in word:
        if i % 2:
            print(c.lower(), end='')
        else:
            print(c.upper(), end='')
        i += c.isalpha()
    print()

changeCase("New Word")  # NeW wOrD
Samwise
  • 68,105
  • 3
  • 30
  • 44
  • Hey, Sam Thank you so much for your help, but I just did the wrong output the one that I want to do is NeW wOrD. Could you help me with it? I don't know if I'm in the right path. – Gustavo Dutra Mar 01 '23 at 10:41
  • Ah, I see -- the way to accomplish that, I think, would be to have a variable that you only increment when the letter `isalpha()`. Probably easier to do with a `for` loop over the string. – Samwise Mar 01 '23 at 15:04
0

You're almost correct! Try out the following code, and see if it works for you!

def changeCase(word):
    for i in range(len(word)):
        if((i%2)==0):
            print(word[i].upper())
        else:
            print(word[i].lower())

changeCase("hello world")

How does it work?

Inside the changeCase() function, we create a for loop (it's easier than a while loop). The loop counts up by 1 each iteration until it reaches the end of the word.
We then check if the current iteration / letter (stored in i) is even. If it is, print the letter in upper case. If it isn't (else), print the lowercase form of the letter.
Finally, call the function with whatever text you want!

0

Other than some typos/minor errors, then main issue with your code is that print inserts a newline after each call. Fixing this (see, e.g., here), one could do:

def changeCase(word):
    i = 0
    while i < len(word):
        if not i%2:
            print(word[i].upper(), end="")
        else:
            print(word[i].lower(), end="")
        i += 1

word = "New Word"

changeCase(word)
NeW WoRd

If you want to ignore blank spaces, then instead you can do:

def changeCase(word):
    i = 0
    j = 0
    while i < len(word):
        if not j%2:  # use j counter
            print(word[i].upper(), end="")
        else:
            print(word[i].lower(), end="")

        # only increment j if not a space character
        if word[i] != " ":
            j += 1
        i += 1

This gives:

word = "New Word"
changeCase(word)
NeW wOrD
Matt Pitkin
  • 3,989
  • 1
  • 18
  • 32
  • Hey, Matt Thank you so much for your help, but I just did the wrong output the one that I want to do is NeW wOrD. Could you help me with it? I don't know if I'm on the right path. – Gustavo Dutra Mar 01 '23 at 10:42
  • @GustavoDutra I've added further to the answer to get the output you require. – Matt Pitkin Mar 01 '23 at 12:15