Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用ASCII更改为大写字母_Python_Ascii_Chr_Ord - Fatal编程技术网

Python 使用ASCII更改为大写字母

Python 使用ASCII更改为大写字母,python,ascii,chr,ord,Python,Ascii,Chr,Ord,我必须创建一个只使用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 = change

我必须创建一个只使用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(文本):
结果=“”
对于文本中的字符:

result+=chr(ord(char)-32)如果ord('a')它自己返回所有字母,但另一个函数涉及isIf。如果我去一个聚会并带着蛋糕返回,我需要返回另一个聚会。在完成之前说
returntext
,因此需要再次调用该函数以获取下一个值。当您再次调用该函数时,您正在重新启动,并且再次仅返回第一个值。
def changeToUpperCase(text):

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