python - Python 函数作用域
问题描述
我将播放器设置为“x”,并定义了一个函数来反转播放器(您将在代码中看到它)。有人可以帮帮我吗?
player = "x"
def update_player(player):
if player == "x":
player = "y"
else:
player = "x"
return player
update_player(player) #called the function with argument player
print(player)
解决方案
您正在打印相同的变量而不是打印函数。像这样编辑您的代码。
player = "x"
def update_player(player):
if player == "x":
player = "y"
else:
player = "x"
return player
k=update_player(player)
print(k)
推荐阅读
- c - 奇怪的 mktime() 行为
- c# - 剃须刀页面中移动设备的页面不同但路由相同
- python - 如何在每 5 个数字后添加换行符?
- html - 在特定模板中分别渲染具有 Status = Accept 或 Reject 的 Django 对象
- reactjs - 如何使用 React Js 更改 SASS 变量值?
- c# - 在 asp.NET Core MVC Web 上启动并行任务
- powershell - 如何将文件从远程服务器(位于另一个域)复制到本地?
- c# - 启动我的应用程序后在 AppData 中创建一个目录
- ios - 本地通知数据
- javascript - 使用 onClick元素