My passwords not printing

Hello,
my password is not printing in the first if statment and i am stuck.
Please help if you can.:

This is my code:

if chk1.get() and chk2.get():
if leng == 3:
password1 = “”.join(random.sample(uppercase_letters, 2))
password2 = “”.join(random.sample(lowercase_letters, 1))
print(“hello”)
password3 = password1 + password2
password4 = “”.join(random.choice(password3))
display_result.delete(0, tkinter.END)
display_result.insert(0, password4)
elif leng == 4:
password1 = “”.join(random.sample(uppercase_letters, 2))
password2 = “”.join(random.sample(lowercase_letters, 2))
password3 = password1 + password2
password4 = string_utils.shuffle(password3)
display_result.delete(0, tkinter.END)
display_result.insert(0, password4)
elif leng == 5:
password1 = “”.join(random.sample(uppercase_letters, 3))
password2 = “”.join(random.sample(lowercase_letters, 2))
password3 = password1 + password2
password4 = string_utils.shuffle(password3)
display_result.delete(0, tkinter.END)
display_result.insert(0, password4)
elif leng == 6:
password1 = “”.join(random.sample(uppercase_letters, 3))
password2 = “”.join(random.sample(lowercase_letters, 3))
password3 = password1 + password2
password4 = string_utils.shuffle(password3)
display_result.delete(0, tkinter.END)
display_result.insert(0, password4)
elif leng == 7:
password1 = “”.join(random.sample(uppercase_letters, 4))
password2 = “”.join(random.sample(lowercase_letters, 3))
password3 = password1 + password2
password4 = string_utils.shuffle(password3)
display_result.delete(0, tkinter.END)
display_result.insert(0, password4)
elif leng == 8:
password1 = “”.join(random.sample(uppercase_letters, 4))
password2 = “”.join(random.sample(lowercase_letters, 4))
password3 = password1 + password2
password4 = string_utils.shuffle(password3)
display_result.delete(0, tkinter.END)
display_result.insert(0, password4)
if chk1.get() and chk2.get() and chk3.get():
qty = leng/3
print(qty)
if isinstance(qty, float):
qty1 = math.ceil(qty)
qty2 = round(qty)
qty34 = qty1 + qty2
qty3 = leng - qty34
password1 = “”.join(random.sample(uppercase_letters, qty1))
password2 = “”.join(random.sample(lowercase_letters, qty2))
password3 = “”.join(random.sample(numbers, qty3))
password5 = password1 + password2 + password3
password4 = string_utils.shuffle(password5)
else:
qty1 = qty
qty2 = qty
qty3 = qty
password1 = “”.join(random.sample(uppercase_letters, qty1))
password2 = “”.join(random.sample(lowercase_letters, qty2))
password3 = “”.join(random.sample(numbers, qty3))
password5 = password1 + password2 + password3
password4 = string_utils.shuffle(password5)
elif chk1.get() and chk3.get():
qty = leng/2
if isinstance(qty, float):
qty1 = math.ceil(qty)
qty3 = leng - qty1
password1 = “”.join(random.sample(uppercase_letters, qty1))
password2 = “”.join(random.sample(numbers, qty3))
password3 = password1 + password2
password4 = string_utils.shuffle(password3)
else:
qty1 = qty
qty3 = qty
password1 = “”.join(random.sample(uppercase_letters, qty1))
password2 = “”.join(random.sample(numbers, qty3))
password3 = password1 + password2
password4 = string_utils.shuffle(password3)
elif chk2.get() and chk3.get():
qty = leng/2
if isinstance(qty, float):
qty2 = math.ceil(qty)
qty3 = leng - qty2
password1 = “”.join(random.sample(lowercase_letters, qty2))
password2 = “”.join(random.sample(numbers, qty3))
password3 = password1 + password2
password4 = string_utils.shuffle(password3)
else:
qty2 = qty
qty3 = qty
password1 = “”.join(random.sample(lowercase_letters, qty2))
password2 = “”.join(random.sample(numbers, qty3))
password3 = password1 + password2
password4 = string_utils.shuffle(password3)
elif chk1.get():
password4 = “”.join(random.sample(uppercase_letters, leng))
elif chk2.get():
password4 = “”.join(random.sample(lowercase_letters, leng))
elif chk3.get():
password4 = “”.join(random.sample(numbers, leng))
else:
upplow = “ABCDEFGHIJKLMNOPQRSTUVWXYZacdefghijklmnopqrstuvwxyzb”
password4 = “”.join(random.sample(upplow, leng))

display_result.delete(0, tkinter.END)
display_result.insert(0, password4)

Hello, @shadowplayz please use code-block when posting code in the forum or use </> toolbar in the editor, like this:

```
copy-paste your code here
```

You can learn more about formatting your code here:
Markdown Tutorial - Code Block

Please read the docs for the secrets module to see a much better, and much shorter, way to generate passwords:

https://docs.python.org/3/library/secrets.html#recipes-and-best-practices

Your password is not printing because you are printing “hello” instead:

print("hello")

Thankyou, i was printing hello to test if my code was running inside the if statement and i appreciate your help Gunung P. Wibisono and Steveng D’Aprano.

def password_generate(leng): # function that creates the password that jumbles and puts together the letters

    uppercase_letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    lowercase_letters = "abcdefghijklmnopqrstuvwxyz"
    numbers = "1234567890"

    if chk1.get() and chk2.get():
        if leng == 3:
            password1 = "".join(random.sample(uppercase_letters, 2))
            password2 = "".join(random.sample(lowercase_letters, 1))
            password3 = password1 + password2
            password4 = "".join(random.choice(password3))
            display_result.delete(0, tkinter.END)
            display_result.insert(0, password4)
        elif leng == 4:
            password1 = "".join(random.sample(uppercase_letters, 2))
            password2 = "".join(random.sample(lowercase_letters, 2))
            password3 = password1 + password2
            password4 = string_utils.shuffle(password3)
            display_result.delete(0, tkinter.END)
            display_result.insert(0, password4)
        elif leng == 5:
            password1 = "".join(random.sample(uppercase_letters, 3))
            password2 = "".join(random.sample(lowercase_letters, 2))
            password3 = password1 + password2
            password4 = string_utils.shuffle(password3)
            display_result.delete(0, tkinter.END)
            display_result.insert(0, password4)
        elif leng == 6:
            password1 = "".join(random.sample(uppercase_letters, 3))
            password2 = "".join(random.sample(lowercase_letters, 3))
            password3 = password1 + password2
            password4 = string_utils.shuffle(password3)
            display_result.delete(0, tkinter.END)
            display_result.insert(0, password4)
        elif leng == 7:
            password1 = "".join(random.sample(uppercase_letters, 4))
            password2 = "".join(random.sample(lowercase_letters, 3))
            password3 = password1 + password2
            password4 = string_utils.shuffle(password3)
            display_result.delete(0, tkinter.END)
            display_result.insert(0, password4)
        elif leng == 8:
            password1 = "".join(random.sample(uppercase_letters, 4))
            password2 = "".join(random.sample(lowercase_letters, 4))
            password3 = password1 + password2
            password4 = string_utils.shuffle(password3)
            display_result.delete(0, tkinter.END)
            display_result.insert(0, password4)
    if chk1.get() and chk2.get() and chk3.get():
        qty = leng/3
        print(qty)
        if isinstance(qty, float):
            qty1 = math.ceil(qty)
            qty2 = round(qty)
            qty34 = qty1 + qty2
            qty3 = leng - qty34
            password1 = "".join(random.sample(uppercase_letters, qty1))
            password2 = "".join(random.sample(lowercase_letters, qty2))
            password3 = "".join(random.sample(numbers, qty3))
            password5 = password1 + password2 + password3
            password4 = string_utils.shuffle(password5)
        else:
            qty1 = qty
            qty2 = qty
            qty3 = qty
            password1 = "".join(random.sample(uppercase_letters, qty1))
            password2 = "".join(random.sample(lowercase_letters, qty2))
            password3 = "".join(random.sample(numbers, qty3))
            password5 = password1 + password2 + password3
            password4 = string_utils.shuffle(password5)
    elif chk1.get() and chk3.get():
        qty = leng/2
        if isinstance(qty, float):
            qty1 = math.ceil(qty)
            qty3 = leng - qty1
            password1 = "".join(random.sample(uppercase_letters, qty1))
            password2 = "".join(random.sample(numbers, qty3))
            password3 = password1 + password2
            password4 = string_utils.shuffle(password3)
        else:
            qty1 = qty
            qty3 = qty
            password1 = "".join(random.sample(uppercase_letters, qty1))
            password2 = "".join(random.sample(numbers, qty3))
            password3 = password1 + password2
            password4 = string_utils.shuffle(password3)
    elif chk2.get() and chk3.get():
        qty = leng/2
        if isinstance(qty, float):
            qty2 = math.ceil(qty)
            qty3 = leng - qty2
            password1 = "".join(random.sample(lowercase_letters, qty2))
            password2 = "".join(random.sample(numbers, qty3))
            password3 = password1 + password2
            password4 = string_utils.shuffle(password3)
        else:
            qty2 = qty
            qty3 = qty
            password1 = "".join(random.sample(lowercase_letters, qty2))
            password2 = "".join(random.sample(numbers, qty3))
            password3 = password1 + password2
            password4 = string_utils.shuffle(password3)
    elif chk1.get():
        password4 = "".join(random.sample(uppercase_letters, leng))
    elif chk2.get():
        password4 = "".join(random.sample(lowercase_letters, leng))
    elif chk3.get():
        password4 = "".join(random.sample(numbers, leng))
    else:
        upp = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        password4 = "".join(random.sample(upp, leng))

    display_result.delete(0, tkinter.END)
    display_result.insert(0, password4)

To preface, I echo @steven.daprano 's suggestion that you should never use the random module for anything involving security (the docs contain a loud, blaring warning to that effect), and rather use the secrets module instead, which is designed for this exact purpose.

Its not clear if you’re saying that it works now, or that it doesn’t, and you’ve merely formatted your code properly for us to be able to examine. Nor is it clear from the code; it looks like you’re using a Tkinter GUI, and by “printing”, maybe you mean outputting the password to the Tkinter GUI but the code snippit doesn’t define or even get passed any, so we can only guess at what might be going on. Fair warning, I’m a Qt user and have never used Tkinter outside of helping beginners on here, so my knowledge of it is pretty is limited.

It appears you are interacting with it via global variables display_result (presumably, what you at least believe to be the Text field in which you want to display your result), and chk1, chk2 and chk3, which I’m guessing are checkboxes that control some aspects of the password generation (which you really should name much less cryptically; after carefully reading through your code to infer their meaning, I’m guessing chk1 controls whether the password uses uppercase, chk2, lowercase, and ch3, numbers).

If I’ve guessed your specific problem correctly above, that you aren’t seeing an output display in some text field you are expecting, due to the lack of context, we can only guess at why, but likely possibilities include:

  • Your function isn’t getting called
  • password4 is empty/None (I don’t think so, but I’d have to trace the very (over)complicated logic to be sure)
  • display_result isn’t what you think it is
  • you are calling the incorrect method on it, or calling it incorrectly
  • there’s some other issue with how you’re using Tkinter.

To narrow all this down is to just have your function print() password4 (which, it seems, is your final generated password, which again you really should name less cryptically, using names rather than numbers for your intermediate forms). If it prints what you expect to the console, you know it is executing and it is generated correctly, and the issue is with display_result. Otherwise, if it doesn’t print, your function didn’t run, and if it prints None/''/etc., the function ran but the password was not generated correctly.

Also, the immense complexity and verbosity in the function above makes it far harder to read, understand, spot errors and fix them, and much easier to make them. Everything done in the 115 lines above can be accomplished in around a dozen just by eliminating all the duplication and redundancy and basing your work on a recipe such as those Steven linked in the secrets module (not to mention, much more secure).

In general, if you see duplicate code on different lines, and especially if they are on nearby lines or are multi-line blocks that are identical, you should be asking yourself how you can call that line only once, because it is almost always possible, and best, to avoid this. In many cases, its as simple as just only putting the one or two lines that differ inside if/else blocks, and moving the rest outside.

Furthermore, there are several logic errors, which this problem exacerbates:

  • Your first if statement executes if the first and the second checkbox is checked, and each sub-branch of it contains duplicate code to clear the current display_result and set it to the generated password4. * You follow it with another if statement, which means one of the other blocks will execute and overwrite the already-generated password, so this whole block has no effect (and ultimately if the user did check only the first and second boxes, the function will act as if only the first was ticked). I’m assuming you meant this to be an elif?
  • Furthermore, the display_result lines are included at the bottom anyway, where they should be, so there’s no need to include them in every elif branch.
  • On top of that, the first branch only handles passwords of certain (very short) lengths, leaving others to fall through and do nothing
  • Some branches handle floats, while some do not, meaning a float may or may not arbitrarily work depending on what combination of checkboxes the user happened to click.

Furthermore, there are issues with your generation algorithm:

  • random.sample is used to pick letters, which is sampling without replacement, meaning at most only one of each character would be chosen, which substantially decreases the quality of generated passwords and limits the length of the generated passwords
  • What’s more, the generated passwords contain equal quantities of the character types, which makes them much more predictable (especially combined with the above). For example, a three-character password with numbers, lower and upper, with replacement, would normally have (26 + 26 + 10) ^ 3 = ≈240 000 possibilities, but instead only has 26 * 26 * 10 * 3! = ≈40 000 possibilities, or 6 times weaker. This gets exponentially worse at longer lengths, on the order of thousands of times weaker or more.

Also, as a sidenote, you don’t need to define the various sets of letters yourself; rather, use the built-in constants from the string module, e.g. string.ascii_lowercase.

What I suggest doing, to simply your code and make it easier to understand. modify and debug, is to first separate your password generation logic from your code the directly interacts with the GUI. For example, you can have a function that takes a length int and upper, lower and number bools, and returns a string with the final password. Then, you have another function that simply calls this function and updates the GUI accordingly. E.g.

def generate_password(length, upper, lower, number):
    # Password generation logic here
    return password

def handle_password_generation(
        display_result, length, chk_upper, chk_lower, chk_number):
    password = generate_password(length, chk_upper.get(), chk_lower.get(), chk_number.get())
    display_result.delete(0, tkinter.END)
    display_result.insert(0, password)

You’d probably want to design handle_password_generation() differently depending on how you have your GUI set up

And for generate_password(), you don’t need separate blocks for every length, or every combination of checkboxes, or even whether length is evenly divisible—what if you need to add a fourth character type, e.g. symbols, or what to support longer lengths?

To avoid all of the issues above, just define the character set first, then pick randomly from it. Thus, your entire >110 line function above, aside from a few tkinter bits that I seperated out above, becomes just 8 lines:

def generate_password(length, upper, lower, number):
    alphabet = ""
    if upper:
        alphabet += string.ascii_uppercase
    if lower:
        alphabet += string.ascii_lowercase
    if number:
        alphabet += string.ascii_digits
    password = "".join(secrets.choice(alphabet) for i in range(length))
    return password

If you’re required to ensure that the generated password contains at least one character of a specific type, or each type, just add an enclosing while loop to keep generating passwords until e.g. set(password) & set(string.ascii_digits), etc.

Thank you, this is going to help.