Ask coding questions

← Back to all posts
2
error
kiki2005 (4)

This piece of code keeps giving me the error "character " not defined and I don't understand what to do with it.Could someone please check through what I have done so far on the code and check for any mistakes.
Thanks.

Commentshotnewtop
6
EchoCoding (249)

You don't need the global keyword. In Python, globals are only needed if a variable is declared within a function. Simply remove the global keyword and the code will run as expected.

Besides that, you haven't called the function menu.

This should work:

print("Welcome to Password Checker")
def menu():
  print("Please enter a number that fits with one of the below.")
  print("1: Password Checker")
  print("2: Password Generator")
  print("3: Quit")
  menuSelection = int(input("..."))

  character = 0

  if menuSelection == 1:
    password = input("please enter a password")
    if password != "":
      if character <= 8:
        print ("this password is too short")
      if character >= 24:
        print("this password is too long")
      if character >= 9 and character <= 23:
        character=0
        character = character + 1
        point = 0 
        length = len(password)
        uppers = [x for x in password if x.isupper()] 
        uppernum = len(uppers) 
        lowers = [x for x in password if x.islower()] 
        lowernum = len(lowers)
        point = point + length + (uppernum * 5) + (lowernum * 5)
        print(point)
  elif menuSelection == 2:
    print("lets generate!") # create the generator here
  elif menuSelection == 3:
    exit()
  else:
    exit()

menu()
1
kiki2005 (4)

Also how would I make the first 5 lines part of a menu

1
YatinBayya (4)

At the top of your code, it should look like this

global character 

It is because of that character only exists in the menu function therefore if you take it outside the function and make it global it can be used anywhere in your application.

Hope that helps :)

1
YatinBayya (4)

If that does help click the check mark next to my answer.

2
kiki2005 (4)

@YatinBayya I put it in before all the code but it said invalid syntax.What should i do now?

2
YatinBayya (4)

@kiki2005 then you have some minor errors but I fixed it for you... I recommend to compare and contrast both and implement better syntax next time

global character
print("Welcome to Password Checker")
def menu():
    print("Please enter a number that fits with one of the below.")
    print("1: Password Checker")
    print("2: Password Generator")
    print("3: Quit")
    menuSelection = int(input("..."))
    if menuSelection == 1:
        password = input("please enter a password")
        for c in password :
            if c.isspace() != True:
              character=0
              character = character + 1
if character<=8:
       print ("this password is too short")
if character>=24:
       print("this password is too long")
else:
  point = 0 
  length = len(password)
  uppers = [x for x in password if x.isupper()] 
  uppernum = len(uppers) 
  lowers = [x for x in password if x.islower()] 
  lowernum = len(lowers)
  point = point + length + (uppernum * 5) + (lowernum * 5)
  print(point)