Nov-06-2019, 04:15 AM
(This post was last modified: Nov-06-2019, 02:31 PM by ichabod801.)
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
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.
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.