I’m currently making a password generator as a personal project but I’ve ran into some bumps. Passwords are generated correctly with the exception of if I do not want symbols and numbers included. I have if statements that are executed depending on if I want symbols and numbers or not but the only statement that runs is the very first that generates a password with symbols and numbers.
This is my function and the call. isSymbols and isNumbers is a variable with an input of “yes”/“Yes” or “no”/“No”. My problem seems to lie within this part of the if statement, I hope someone can find the solution for me.
Hello! Unfortunately, we don’t seem to be able to see the code; would you mind posting it outside a codebyte, please? Make sure format it correctly.
I copied your code from the Codebyte to notepad because my browser was having trouble displaying that.
your code
import random
from tkinter.messagebox import YES
#Declaring password characters
lowerCase = "abcdefghijklmnopqrstuvwxyz"
upperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
numbers = "0123456789"
symbols = ".,?/~'@;:!£$%^&*()[]-_=+"
#Declaring password paramters
isSymbols = input("Will your password use symbols? ")
isNumber = input("Will your password use numbers? ")
passLength = int(input("What is your desired password length? "))
print(isSymbols)
print(isNumber)
print(passLength)
#Delcaring the function to generate the password
def randomPassword(lowerCase, upperCase, numbers, symbols, isSymbols, isNumber, passLength):
if isSymbols is "yes" or "Yes" and isNumber is "yes" or "Yes":
string = lowerCase + upperCase + numbers + symbols
length = passLength
password = "".join(random.sample(string, length))
return print(password)
elif isSymbols is "no" or "No" and isNumber is "yes" or "Yes":
string = lowerCase + upperCase + numbers
length = passLength
password = "".join(random.sample(string, length))
return print(password)
elif isSymbols is "no" or "No" and isNumber is "no" or "No":
string = lowerCase + upperCase
length = passLength
password = "".join(random.sample(string, length))
return print(password)
else:
return print("Error please try again")
randomPassword(lowerCase, upperCase, numbers, symbols, isSymbols, isNumber, passLength)
I don’t know if this will work, but you can try changing the if statements …
Change:
if isSymbols is "yes" or "Yes" and isNumber is "yes" or "Yes":
to
if (isSymbols is "yes" or isSymbols is "Yes") and (isNumber is "yes" or isNumber is "Yes"):
although personally, I like:
if (isSymbols == "yes" or isSymbols == "Yes") and (isNumber == "yes" or isNumber == "Yes"):
so that its testing whether isSymbols
is "Yes"
instead of just testing whether "Yes"
is True
(since "Yes"
is always True
, because a non-empty string always evaluates to True
[It’s truthy] ).
And similarly for the elif
statements.
Additionally,
the print function doesn’t return anything,
so you may want to change
return print(password)
to:
print(password)
return password
Works thank you very much, I thought I tried something like this before and it didnt work. Clearly I didnt quite do your suggestion.