Skip to main content
 首页 » 编程设计

python之创建小写单词(将大写字母转换为小写字母)的功能无法按预期工作

2024年10月25日8think

我不明白为什么下面的 Python 代码打印出一个包含大写字母的句子...请解释! :)

def lower(text): 
    text = text.lower() 
 
example = "This sentence has BIG LETTERS." 
lower(example) 
print(example) 

输出将是:

This sentence has BIG LETTERS. 

请您参考如下方法:

你的函数不返回任何东西,你需要返回这个词:

def lower(text): 
    text = text.lower() 
    return text 

演示:

>>> example = "This sentence has BIG LETTERS." 
>>> lower(example) 
'this sentence has big letters.' 

如果您的意思是为什么以下内容不起作用:

lower(example) 
print(example) 

您需要知道函数内的变量具有局部作用域,并且通过调用 lower(example) example 不会在全局范围内改变!