-2

I have to create a function that changes lower case letters to upper case letters using only the ord and chr functions.

This is what I have so far, but the problem is it is not returning all the letters, only the first letter.

def changeToUpperCase(text):

for i in text:
    i = ord(i) - 32
    text = chr(i)


    return text

def main():

text = input("Please type a random sentence.")



text = changeToUpperCase(text)
print("Step 2 -> ", text)
Meena joumaa
  • 21
  • 2
  • 4
  • it does return all the letters on its own but the other function involved is – Meena joumaa Mar 19 '16 at 19:20
  • If I go to a party and return with a cake, I need to go back to return with another. You say `return text` before you are done, so you need to call the function again to get the next value. When you call the function again, you are restarting and you again return just the first value. – zondo Mar 19 '16 at 19:31

2 Answers2

2

Here is a solution:

def changeToUpperCase(text):

    result = ''
    for char in text:
       result += chr(ord(char) - 32) if ord('a') <= ord(char) <= ord('z') else char
    return result
0
def changeToUpperCase(text):
    new_text = ""
    for i in text:
        i = ord(i) - 32
        new_text = new_text + chr(i)    
    return new_text

you need to wait to return until you have parsed the whole thing

Joran Beasley
  • 110,522
  • 12
  • 160
  • 179