-1
import re
score = 0

capital_letters = r'[A-Z]'
a = re.compile(capital_letters)

lowercase_letters = r'[a-z]'
b = re.compile(lowercase_letters)

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

def upper_score(test_string, aScore):
  if re.match(a, test_string):
    aScore = increase_score(5, aScore)
    print (aScore)
    print("UPPERCASE")
  else:
     print("The password needs capital letters for a higher score")


def lower_score(test_string, aScore):
    if re.match(b, test_string):
      aScore = increase_score(5, aScore)
  print (aScore)
  print("LOWERCASE")
else:
  print("The password needs lowercase letters for a higher score")

password = input("Enter a password to check")
upper_score(password, score)
lower_score(password, score)

If I input all upper case letters I get this output:

5
UPPERCASE

The password needs lowercase letters for a higher score If I input all lower case letters I get this output:

The password needs capital letters for a higher score 
5 
LOWERCASE

When i mix upper case annd lowercase i get this output:

 5
 UPPERCASE
 5
 LOWERCASE

Even though there are both uppercase and lowercase letters the score is still 5 instead of 10.

I want the score to be cumulative and build ontop of each other.

  • You changed only the local copy of the score; you never changed `score` in your main program. – Prune Jan 03 '18 at 21:38

1 Answers1

0

When you send the score to the function:

upper_score(password, score)

you are actually sending a copy of the score. In your example it recieves score which has a value of 0, and creates a new variable aScore with also a value of 0, so when you change aScore you don't actually change score. (Be careful, even if they had the same name they still wouldn't be the same variables.)

There are a few ways to do what you are trying to do. The simpler way would be to simply make the function return aScore and there you can add it to your score.

def upper_score(test_string, aScore):
  if re.match(a, test_string):
    aScore = increase_score(5, aScore)
    print (aScore)
    print("UPPERCASE")
  else:
     print("The password needs capital letters for a higher score")
  return aScore


def lower_score(test_string, aScore):
    if re.match(b, test_string):
      aScore = increase_score(5, aScore)
      print (aScore)
      print("LOWERCASE")
    else:
      print("The password needs lowercase letters for a higher score")
    return aScore

password = input("Enter a password to check")
score += upper_score(password, score)
score += lower_score(password, score)
print("total score: " + score)
Liora Haydont
  • 1,252
  • 1
  • 12
  • 25