Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Function and Random
#1
I am making a function that designs a password that must follow specific criteria. The input is the length of the password, the output is the password.

I normally start my programming NOT in function form (makes it easier for me). I assigned a variable inputlength and just used that. My code worked perfectly.

Now though, once I insert def randompassword(inputlength) and if __name__ == '__main__': I am having issues. The password generated each time does NOT change and it does not follow the length. I do not know if this is because of the placement of import random; I have tried with this within and outside of the function boundaries. I will attach both my normal/non-function code first and then my function code. Thanks in advance.

NON-Function, i.e. assigning input length at top of code
import random
inputlength=int(input("What is the length? "))
password=""
randomvariable=random.randint(1,2)


if randomvariable==1:
    password+=random.sample('ABCDEFGHIJKLMNOPQRSTUVWXYZ',1)[0]
    password+=random.sample('abcdefghijklmnopqrstuvwxyz',1)[0]
if randomvariable==2:
    password+=random.sample('abcdefghijklmnopqrstuvwxyz',1)[0]
    password+=random.sample('ABCDEFGHIJKLMNOPQRSTUVWXYZ',1)[0]


while len(password)<inputlength-1:
    if "$" not in password:
        if "@" not in password:
            if "!" not in password:
                password+=random.sample('$@!',1)[0]

        
    password+=random.sample('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890',1)[0]
           
password+=random.sample('1234567890',1)[0]
    
print(password)
WITH FUNCTION
import random
def randompassword(inputlength):
    
    password=""
    randomvariable=random.randint(1,2)


    if randomvariable==1:
        password+=random.sample('ABCDEFGHIJKLMNOPQRSTUVWXYZ',1)[0]
        password+=random.sample('abcdefghijklmnopqrstuvwxyz',1)[0]
    if randomvariable==2:
        password+=random.sample('abcdefghijklmnopqrstuvwxyz',1)[0]
        password+=random.sample('ABCDEFGHIJKLMNOPQRSTUVWXYZ',1)[0]


    while len(password)<inputlength-1:
        if "$" not in password:
            if "@" not in password:
                if "!" not in password:
                    password+=random.sample('$@!',1)[0]

        
        password+=random.sample('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890',1)[0]
           
    password+=random.sample('1234567890',1)[0]
    
    return(password)
if __name__ == '__main__':
    inputlength=int(input("What is the length? "))
    print(password)
What is the length? 5
Wq$1b2Q61


I got the above output for length of 5, 9, and 10. When I assign inputlength it works fine and changes each time.

Update: I started receiving an error because "password" is undefined. I moved print(password) INTO the function and now the function asks me for the length and then does nothing.
Reply
#2
(Nov-06-2019, 04:15 AM)mkmoloney Wrote: Update: I moved print(password) INTO the function and now the function asks me for the length and then does nothing.

Maybe because you are not calling your function and printing the returned value?
Where shall the variable password beneath "if __name__ == '__main__':" come from?
Reply
#3
I was not calling the function correctly. I needed to add


if __name__ == '__main__':
    inputlength=int(input("What is the length? "))
    print(randompassword(inputlength))
Thanks!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020