1

I'm trying to call a function which I've defined but the code fails to call it. Does it matter where the function is defined?

def register():
    """Register user"""
    if request.method == "GET":
        return render_template("register.html")
    else:
        if not request.form.get("username"):
            return apology("must provide username", 403)
        elif not request.form.get("password") or not request.form.get("confirmation"):
            return apology("must provide password", 403)
        elif request.form.get("password") != request.form.get("confirmation"):
            return apology("passwords don't match")
        username = request.form.get("username")
        password = request.form.get("password")
        password_check(password)

def password_check(passwd):

Symbols =['$', '@', '#', '%']

if len(passwd) < 8:
    return apology("length should be at least 6", 403)

if len(passwd) > 20:
    return apology('length should be not be greater than 8', 403)

if not any(char.isdigit() for char in passwd):
    return apology('Password should have at least one numeral', 403)

if not any(char.isupper() for char in passwd):
    return apology('Password should have at least one uppercase letter', 403)

if not any(char.islower() for char in passwd):
    return apology('Password should have at least one lowercase letter', 403)

if not any(char in Symbols for char in passwd):
    return apology('Password should have at least one of the symbols $@#%', 403)
  • Does defining the function before calling register work? – Vsjain Sep 24 '20 at 15:17
  • I get the error TypeError: password_check() missing 1 required positional argument: 'passwd' – Varun Beluguli Sep 24 '20 at 16:58
  • Does it function as required in a separate file? – Vsjain Sep 25 '20 at 10:00
  • umm why do I need a separate file? – Varun Beluguli Sep 25 '20 at 10:49
  • To test the function separately. – Vsjain Sep 25 '20 at 11:23
0

I had exactly the same issue, and the function doesn't work neither above register nor in a separate file (helpers).

It only works within the register not as a function but directly as a piece of code.

Does anybody have any suggestion of what might be the reason?

Thanks!

0

Look at your code:

def password_check(passwd):

Everything below the code is not properly indented. Python is super-sensitive to proper indentation in order to put variables and statements within the proper scope. Another problem I see is that what happen when all your error checks are true, what does the function do? You might have to work on that also.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .