Phnx Drm
Phnx Drm

Reputation: 1

function returns none instead of a tuple

I am using python 2.7. I wrote a code to generate passwords. For doing this, I used the random module to generate how many characters of different types(uppercase, lowercase, special and numbers) to be used to generate a password of a given length. When a wrote a function for this, it was supposed to return a tuple, but it returns None. Why is it happening?

I tried the casual debugging method of putting print statements in between and figuring out where it went wrong. Everything works just fine, except it returns None.

def passlen(a):
    """Gives length of different characters to be put into passwords"""
    uplen=random.randrange(0, a)
    lwlen=random.randrange(0, a)
    speclen=random.randrange(0, a)
    nmbrlen=random.randrange(0, a)
    if uplen+lwlen+speclen+nmbrlen==a:
        print (uplen, lwlen, speclen, nmbrlen)
        return(uplen, lwlen, speclen, nmbrlen)
    else:
        passlen(a)

x=input("how many characters in the password?")

print(passlen(x))

Expected results are 4-tuples, but it gives None instead.

Upvotes: 0

Views: 692

Answers (3)

Veron Gamer
Veron Gamer

Reputation: 1

import random

def passlen(a):
    """Gives length of different characters to be put into passwords"""
    uplen = random.randrange(0, a)
    lwlen = random.randrange(0, a)
    speclen = random.randrange(0, a)
    nmbrlen = random.randrange(0, a)
    if uplen + lwlen + speclen + nmbrlen == a:
        print("Length of Uppercase:", uplen)
        print("Length of Lowercase:", lwlen)
        print("Length of Special characters:", speclen)
        print("Length of Numeric characters:", nmbrlen)
        return (uplen, lwlen, speclen, nmbrlen)
    else:
        return passlen(a)

x = int(input("How many characters in the password? "))  # Convert input to integer

print("Generated Password Lengths:", passlen(x))

Upvotes: -1

Tim Pietzcker
Tim Pietzcker

Reputation: 336148

So you want four random numbers that add to a? Of course you can try choosing four random numbers until you find a set that adds up to a, but that might take a while for large values of a (and you definitely don't want to do this recursively).

Much better to choose three split points between 0 and a:

def passlen(a):
    splits = sorted([random.randrange(0,a) for _ in range(3)])
    uplen = splits[0]
    lwlen = splits[1] - uplen
    speclen = splits[2] - uplen - lwlen
    nmbrlen = a - uplen - lwlen - speclen
    return uplen, lwlen, speclen, nmbrlen

Upvotes: 1

Phnx Drm
Phnx Drm

Reputation: 1

Thanks to Kamiccolo for trying to help out.

The function should look like this:

def passlen(a):
    """Gives length of different characters to be put into passwords"""
    uplen=int(random.randrange(0, a))
    lwlen=int(random.randrange(0, a))
    speclen=int(random.randrange(0, a))
    nmbrlen=int(random.randrange(0, a))
    bab=(uplen, lwlen, speclen, nmbrlen)
    if uplen+lwlen+speclen+nmbrlen==a:
        return bab
    else:
        return passlen(a)

A duplicate thread also helped me in this.

Upvotes: 0

Related Questions