Reputation: 26
I created a random password generator, but, when i enter the data, it keeps showing this error:
Traceback (most recent call last):
File "main.py", line 25, in <module>
password += random.choice(all[num])
File "/usr/lib/python3.8/random.py", line 288, in choice
i = self._randbelow(len(seq))
TypeError: object of type 'int' has no len()
#Password Generator Project
import random
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
symbols = ['!', '#', '$', '%', '&', '(', ')', '*', '+']
print("Welcome to the PyPassword Generator!")
nr_letters= int(input("How many letters would you like in your password?\n"))
nr_symbols = int(input("How many symbols would you like?\n"))
nr_numbers = int(input("How many numbers would you like?\n"))
password = ""
all = [letters, numbers, symbols, nr_letters, nr_numbers, nr_symbols]
lenght = nr_letters + nr_numbers + nr_symbols
for x in range (0, lenght):
num = int(random.randint(0, len(all)/2))
password += random.choice(all[num])
all[int(num + len(all)/2)] -= 1
if nr_letters == 0:
ended = all.index(nr_letters)
all.remove(ended, int(ended - len(all)/2))
if nr_numbers == 0:
ended = all.index(nr_numbers)
all.remove(ended, int(ended - len(all)/2))
if nr_symbols == 0:
ended = all.index(nr_symbols)
all.remove(ended, int(ended - len(all)/2))
print(password)
Upvotes: 0
Views: 1562
Reputation: 19
Why not use chr(randint(33, 126))
to generate letters, numbers and symbols
Upvotes: 0
Reputation: 10819
You have defined all
to be this:
all = [letters, numbers, symbols, nr_letters, nr_numbers, nr_symbols]
letters
, numbers
and symbols
are lists, but nr_letters
, nr_numbers
and nr_symbols
are integers. That means that some of the items in all
are lists, and some are integers.
Later, in your for-loop, you use random.randint
to generate a random index to use with all
:
num = random.randint(0, len(all)/2)
However, random.randint
includes both endpoints, so the possible indices are zero through three (inclusive) - that's four indices. You then use that index to select one of the items in all
when you do random.choice(all[num])
. When num
is 3
, you effectively do random.choice(nr_letters)
.
Upvotes: 3