首页 > 解决方案 > 为什么这个函数返回 LinkedIn 而不是 Amazon?

问题描述

为什么这个函数返回 LinkedIn 而不是 Amazon?

def hey(location):
        location = "LinkedIn"
        answer = "Welcome to " + location
        return answer
        
print(hey("Amazon"))

标签: pythonfunctionlocal

解决方案


location因为您在"LinkedIn"函数内部声明。

如果您希望"LinkedIn"在没有参数输入的情况下作为默认值location,请尝试:

def hey(location="LinkedIn"):
    return "Welcome to " + location

推荐阅读