我已经使用 Python 一段时间了,但我从未真正学会如何传递参数,而且我养成了使用全局变量的坏习惯,但现在我正在努力学习,但进展并不顺利.有人能在这里发现我做错了什么吗:
代码的简化版本:
clickLoad = 0
def get_length(clickLoad):
listLength = 305 #(Sample Value)
if listLength < 101:
clickLoad = 0
elif 100 < listLength < 201:
clickLoad = 1
elif 201 <= listLength < 301:
clickLoad = 2
elif 301 <= listLength < 401:
clickLoad = 3
return clickLoad
def load_more(clickLoad):
print(clickLoad) # Testing value, prints 0
while clickLoad < 0:
# Do some stuff
clickLoad -= 1
return clickLoad
get_length(clickLoad)
load_more(clickLoad)
clickLoad 的值似乎永远不会从 0 改变。当 listLength = 305 时,我希望它是 3。
请您参考如下方法:
clickLoad = 0
def get_length(clickLoad):
listLength = 305 #(Sample Value)
if listLength < 101:
clickLoad = 0
elif 100 < listLength < 201:
clickLoad = 1
elif 201 <= listLength < 301:
clickLoad = 2
elif 301 <= listLength < 401:
clickLoad = 3
return( clickLoad)
def load_more(clickLoad):
print(clickLoad) # Testing value, prints 0
while clickLoad < 0:
# Do some stuff
clickLoad -= 1
return clickLoad
s = get_length(clickLoad)
load_more(s)
尝试这个,因为你没有在调用之前将函数返回的值存储到下一个函数,因此它采用全局值
