为什么它打印 0 而不是 5? 我找不到我的逻辑错误?
score = 0
def pass_score(test_string, aScore):
if re.match(a, test_string):
increase_score(5, score)
print (aScore)
def increase_score (aValue, aScore):
aScore += aValue
最佳答案
第一种方法,没有全局变量,返回值:
def increase_score (aValue, aScore):
aScore += aValue
return aScore
def pass_score(test_string, aScore):
if re.match(a, test_string):
aScore = increase_score(5, aScore)
print (aScore)
第二种方法,使用global变量:
score = 0
def increase_score (aValue): #don't need to receive score, I've it.
global score
score += aValue
def pass_score(test_string):
global score
if re.match(a, test_string):
increase_score(5)
print (score)
我想你需要两者的结合。无论如何,您的代码此时看起来有点脏,混合了本地变量和全局变量。
关于python - 如何在Python中使用def和参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48052710/