Home > database >  I am trying to create a password generator and i keep getting this error: "TypeError: can only
I am trying to create a password generator and i keep getting this error: "TypeError: can only

Time:06-12

I am a beginner and I am trying to create a random password generator. I get a entry box and a button, but after entering a number(the password length) I get an error, it is supposed to return a password.

Full error:

Exception in Tkinter callback

Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/tkinter/init.py", line 1921, in call return self.func(*args) File "/Users/denis/PycharmProjects/pythonProject/the first/Things/Random Password Generator.py", line 19, in password_generator random_password = character_list[random.randint(0, 91)] TypeError: can only concatenate tuple (not "str") to tuple

code:

import random
from tkinter import *
from typing import Tuple

root = Tk()

input_box = Entry(root, width=22, borderwidth=2, bg="#000000")
input_box.grid(row=0, column=0, columnspan=2)
input_box.insert(0, 'Enter the password length:', )
random_password = ()

def password_generator():
    character_list = ("""QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm
    1234567890`~!@#$%^&*()_ []\{}|;':",./<>?""")
    num_of_runs = 1
    random_password = ()
    password_length = int(input_box.get())
    while num_of_runs <= int(password_length):
        random_password  = character_list[random.randint(0, 91)]
        num_of_runs  = 1
    Label(random_password).grid(row=2, column=0)


my_button = Button(root, text="Click here for your password", 
               command=password_generator, fg="#000000", bg="white", )
my_button.grid(row=1, column=0, columnspan=2)

root.mainloop()

CodePudding user response:

random_password = () is defining an empty tuple, you probably intend to use random_password ='' if you're wanting to append characters to it.

However, better yet would be to use random.choices and str.join:

random_password = ''.join(random.choices(character_list, k=password_length))

CodePudding user response:

Read your error , it says you are trying to add string to a tuple which is causing the exception. If u review your code you can see the problem in the following lines:

    random_password = () # initialized an empty tuple 
    .....
    random_password  = character_list[random.randint(0, 91)] # adding string to the tuple

initialize your "random_password" variable to empty string ("") and you should be good

  • Related