jeudi 24 juin 2021

What is causing the message "IndexError: list index out of range"?

I am comfortable with python but I can't figure out why this error is showing up because of my code:

import random
inp = input('Enter: ')
inp = str(inp)
char = 0
key = []
list = list('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%&*?+-')

if inp == 'New':
    while char < len(list):
        rand = random.randint(0, len(list))
        key.append(list[rand])
        char = char + 1
print(key)

The code won't always print the list "key" and instead give me the error "IndexError: list index out of range". How do I fix this?




Aucun commentaire:

Enregistrer un commentaire