when i chose 2 letters, 2 symbols and two numbers it works, but when i chose per example 40 numbers, 40 sybmols and 40 numbers, it doesnt work, say that
when i chose 2 letters, 2 symbols and two numbers it works, but each number dont work
say that:
Traceback (most recent call last): File "main.py", line 866, in symbols_password = symbols[random.randint(0,len(symbols))] IndexError: list index out of range
it says that this line is line isnt on the range letters[random.randint(0,len(letters))]
my code
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(f"How many symbols would you like?\n"))
nr_numbers = int(input(f"How many numbers would you like?\n"))
letters_password = ""
symbols_password = ""
number_password = ""
if int(nr_letters) <= 50 and int(nr_symbols) <= 50 and int(nr_numbers) <= 50:
for generator_letters in range(0,int(nr_letters)):
letters_password = letters[random.randint(0,len(letters))]
for generator_symbols in range(0,int(nr_symbols)):
symbols_password = symbols[random.randint(0,len(symbols))]
for generator_numbers in range(0,int(nr_numbers)):
number_password = numbers[random.randint(0,len(numbers))]
else:
if nr_letters > 50 and nr_symbols > 50 and nr_numbers > 50:
print("you chose too many letters,symbols and numbers.\n The maximum is 50 of each")
elif nr_letters > 50 and nr_symbols > 50 :
print("you chose too many letters and symbols.\n The maximun us 50 of each")
elif nr_letters > 50 and nr_numbers > 50:
print("you chose too many letters and numbers.\n The maximun us 50 of each")
elif nr_symbols > 50 and nr_numbers > 50:
print("you chose too many symbols and numbers.\n The maximun us 50 of each")
elif nr_numbers > 50:
print("you chose too many numbers, the maximum is 50")
elif nr_letters > 50:
print("you chose too many letters, the maximum is 50")
elif nr_symbols > 50:
print("you chose too many smybols, the maximum is 50")
password =(str(letters_password) str(symbols_password) str(number_password))
print(password)
CodePudding user response:
Yeah, you're issue is a tiny quirk of the random.randint()
function. random.randint()
chooses a random integer between and including the first and second parameters that you give it, meaning that it could actually choose the second parameter that you give it as an output. Seeing that len(symbols)
actually is not an index of symbols, you are bound to run into that IndexError
on occasion. Each time you call random.randint()
, subtract 1
from your second parameter and you should be good. For example:
symbols_password = symbols[random.randint(0,len(symbols)-1)]
CodePudding user response:
From the random module docs:
random.randint(a, b)
Return a random integer N such that a <= N <= b. Alias for randrange(a, b 1).
So the range includes len(symbols)
.
Since python uses 0 based indexing, symbols[len(symbols)]
is an IndexError
.
Solution: use random.randrange() instead, or subtract 1 from the second argument to randint().
CodePudding user response:
I think what's wrong here that you're trying to use this method with the string data you have in your lists
random.randint() which what it does is : random.randint(a, b) Return a random integer N such that a <= N <= b. Alias for randrange(a, b 1).
So try to use the random.choice(seq)
method instead
and refere to Random library DOCS.py for more info about the random library