mineth.silva
mineth.silva

Reputation: 3

why don't I get the output

This code is used to check whether a username is valid. I don't get any output even if I call the function.

"""
for the password to be valid;
 must have only 10 characters
 have at least 1 uppercase letters
 must have 6 lowercase letters
 others must be numeric
"""
ucase=0
lcase=0
num=0
#length check
def lencheck():
    global length
    length=len(uname)

#Ucase check
def uppercase():
    for x in range(length):
        if uname[x]>='A' or  uname<='Z':
            global ucase
            ucase=ucase+1

#Lcase check
def lowercase():
    for x in range(length):
        if uname[x]>='a' or uname[x]<='z':
            global lcase
            lcase=lcase+1

#numeric check
def numeric():
    for x in range(length):
        if (uname[x])>='0' or (uname[x])<='9':
            global num
            num=num+1

#main program
def main():
    lencheck()
    if length==10:
        uppercase()
        lowercase()
        numeric()
        numcheck=length-(ucase+lcase)
        if ucase>=1 and lcase>=6 and num==numcheck:
            print ("username valid")
            valid=True
            return valid
    else:
        valid=False
        print("username invalid")
        return valid

uname=str(input("enter username:"))
main() 

Upvotes: 0

Views: 89

Answers (2)

njavig
njavig

Reputation: 112

I have tried your code with several entries, and at least for the given cases, it output the result correctly.
However, the problem is that, you are missing else: for the nested condition - if ucase>=1 and lcase>=6 and num==numcheck:
To avoid this, it is good to have only one exit point (return) on your function.

def main():
    valid=False # It will be remained as False unless all the conditions are met
    lencheck()
    if length==10:
        uppercase()
        lowercase()
        numeric()
        numcheck=length-(ucase+lcase)
        if ucase>=1 and lcase>=6 and num==numcheck:
            valid=True
    print("username %s" % ('valid' if valid else 'invalid'))
    return valid

Also, change all or into and in other functions

Upvotes: 2

Utpal Kumar
Utpal Kumar

Reputation: 300

The problem is here:

if ucase>=1 and lcase>=6 and num==numcheck:

if we give qwertyuiop as a random input, num comes to be 10 and numcheck comes to be -10, and both are not equal and hence the program does not enters the block of code inside it. Since there is no else block for it, the program exits the main function without any output.

If you want to check that the input provided is a number or not, I would suggest you to use .isdigit, also if (uname[x])>='0' or (uname[x])<='9': will only check if the input is equal to or greater than or less than or equal to 0 or 9 respectively and for other digits.

def numeric():
    for x in range(length):
        if uname[x].isdigit():
            global num
            num=num+1

The same things applies for uppercase() and lowercase().

To check upper case you can try this:

#Ucase check
def uppercase():
    upperCase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    for x in range(length):
        if uname[x] in upperCase:
            global ucase
            ucase=ucase+1

#Lcase check
def lowercase():
    lowerCase = 'abcdefghijklmnopqrstuvwxyz'
    for x in range(length):
        if uname[x] in lowerCase:
            global lcase
            lcase=lcase+1

Hope you were looking for this.

Upvotes: 0

Related Questions