3 回答
白板的微信
TA贡献1883条经验 获得超3个赞
我认为您对全局变量、返回语句和函数感到困惑。尝试一下并阅读我的内联评论:
x=3
def calculateOO(y):
global x
x=3*y
def calculateXX(z):
y=3*z
return y
print(calculateOO(2))
#prints None, as the function has no return statement
print(x)
#prints 6, as we set x to be global in the calculateOO() function above
print(calculateXX(2))
#prints 6, as we return the value inside the function
print(y)
#causes an error, as we did not set a global y
守着星空守着你
TA贡献1799条经验 获得超8个赞
您还可以执行以下操作:
x=3
def calculateOO(y):
global x
x=3*y
return print((3*y),x)
calculateOO(2)
希望这个解决方案对您有所帮助
添加回答
举报
0/150
提交
取消