使用 ASCII 更改为大写字母



>我必须创建一个函数,仅使用 ord 和 chr 函数将小写字母更改为大写字母。

这就是我到目前为止所拥有的,但问题是它没有返回所有字母,只返回第一个字母。

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)

这是一个解决方案:

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

您需要等到解析完整个内容后再返回

最新更新