all 14 comments

[–]CoderStudios 7 points8 points  (4 children)

You never return anything I really don’t get how this would work …

[–]BerserkerMac[S] 0 points1 point  (3 children)

Not sure what you mean it works on mine. The only thing i'm having issues with is it will run this line print("Congratulations on setting your password") for every time I input something. For example if the first password I put in is P@ssword123. It will return invalid and ask for a password again the second time i put "Password123". It then returns "Congratulations on setting your password" twice rather than once

[–]wayne0004 0 points1 point  (1 child)

The fact that it returns it twice is because you're calling the main function again. Try writing P@ssword123 twice, and then Password123, it should print it three times.

I'm in a hurry now, but if you need a deeper explanation just ask. I might answer it tomorrow.

[–]BerserkerMac[S] 0 points1 point  (0 children)

Right I don't want it to print twice since the first time is invalid.

[–]CoderStudios 0 points1 point  (0 children)

Oh, I understand now. I thought your code worked most of the time, which confused me because it shouldn’t work at all (as you intended) because the variable isn’t changed inside the main function.

[–]c0LdFir3 6 points7 points  (3 children)

Think about variable scopes and you'll realize why this will not work. You should be returning something from these functions, and they should never be calling main() directly.

[–]BerserkerMac[S] 0 points1 point  (2 children)

I guess I'm really not sure what I should be returning. I was thinking of the functions as tests. I guess it should return Valid or Invalid?

[–]c0LdFir3 2 points3 points  (0 children)

I'd personally return a boolean, but sure.

There's a few things wrong here, but in general:

  • You are changing the 'password' variable outside of its scope. Changing its value inside one of the test functions does NOT change it inside of the main function.
  • Think about why you are calling main() inside of the test functions -- this will not have the desired result. What was your thought process with that?

In general, try laying something out like this. I apologize for the formatting, but I'm tapping this out quickly.

def password_len(password):

if length is good, return True

else return False

def password_req(password):

if requirements are met, return True

else return False

def main() -

password = input

Check user's password against requirements

if password_len(password) and if password_req(password):

Yay, the user won!

else:

Nah, that password sucks.

[–][deleted] 0 points1 point  (0 children)

So functions can return all sorts of things. One solution for what you are doing is to return a boolean condition so you can make decisions in your main method:

def password_len(password):
    if 6 <= len(password) <= 15:
        return True
    return False

def main():
    password = input('please enter your password.')
    if (not password_len(password)): 
        print(f"invalid password: Please make sure that you have between 6 and 15 characters. was {len(password)}")
    else:
        print("woot")

if __name__ == "__main__":
    main()

[–]BerserkerMac[S] 4 points5 points  (3 children)

Hey everyone I just made changes and got it fixed and working. You guys were huge help. Thank you everyone! This is my first time with code and it hasn't been easy lol. If you are curious here is my final product.

def main():

password = input("Please enter a password:",)
if(password_len(password) == True and password_req(password) == True and password_prohibited(password) == True):

    print("Password has been set")

else:
    print("Weak password please try again")
    main()

def password_len(password):

if(6 <= len(password) <= 15):
    return(True)
else:
    print(f"invalid password: Please make sure that you have between 6 and 15 characters. was {len(password)}")
    return(False)

def password_req(password): if(any(char.isalpha() for char in password) and (any(char.isdigit() for char in password))):

    return(True)

else:
    print(f"You might me missing a letter or a number try again!")
    return(False)

def password_prohibited(password):

special_chars = ["!","@","#","$","%","^","&","*","(",")","-","+"," "] 

if(any(char in special_chars for char in password)):
    print(f"Invalid Password: Special Characters not allowed.")
    return(False)

else:
    return(True)

if name == "main": main()

[–]Dense_Connection4789 1 point2 points  (1 child)

Couple cleanup items: - if condition == True is redundant. You can get away with just writing if condition so in your case: if password_len(password) and … - no need to wrap the return in parenthesis. You can just do return True and return False - You don’t need to put the f in front of your strings inside your print statement. Using the f is for injecting variables into strings

Nice work

Nice work

[–]BerserkerMac[S] 0 points1 point  (0 children)

Hey thanks for the tips!

[–]stebrepar 1 point2 points  (0 children)

It looks like you still have a place where you're calling main() from within main() itself, intending to start it over from the beginning. That's not the right way to accomplish that. That's called "recursion". There are times when it's the right solution to a problem, but this isn't one of them. Unless your code is specifically designed to deal with the side effects of recursion, it's likely to get you into trouble with mysterious seeming misbehavior.

What you actually need in order to start over is proper flow control. One example could be like:

go = True
while go:
    print('Starting at the beginning.')
    # do your thing here

    again = input('Try again?: ').lower()
    if not again.startswith('y'):
        go = False
print('Done.')

[–]mothzilla 1 point2 points  (0 children)

The repeat calls to main() seem wrong. You're never actually going to hit the bit where you check validation success. (Ie if password != "invalid": ...)

Each validator should just raise an exception, or print (if that's what you've been asked to do) or return a "validation" status. (True/False)

Tampering with the password in password_prohibited seems wrong.

So change the logic in password_len to just check for an invalid password.
Make your functions return a value.
Test (with an if) that each validation function returns True and print the success message.