1

why does it print 0 and not 5? i cant find the error in my logic?

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
0

2 Answers 2

2

First approach, without the global var, returning value:

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)

Second approach, using global var:

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)

I guess you need a mix of both. Any case, your code looks a bit dirty at this moment, mixing local and global vars.

Sign up to request clarification or add additional context in comments.

1 Comment

YOU ARE AMAZING HAVE A HAPPY NEW YEAR
1

You could do globals, but don't. Instead, return the value.

def increase_score (aValue, aScore):
    return aScore += aValue

def pass_score(test_string, aScore):
    if re.match(a, test_string):
        aScore += increase_score(5, aScore)
    print (aScore)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.