Code is throwing error when trying do to something using a method I've successfully it used before

Bug description:
I continue to get the “list assignment index out of range” error when after comparing to another program I’ve made, there’s no major differences that would cause this issue. All the other variables are already defined.

Expected vs Current Behavior:
The for loop should change every value of the string list to either a symbol, number, uppercase character, or lowercase character. But throws an error saying that the index used doesn’t exist.

string = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "12", "13", "14", "15"]

for i in range(15):
  character_types = (1, 2, 3, 4)
  character_type = random.choice(character_types)

  if character_type == 1:
    string[i] = random.choice(lowercase_character_list)
  elif character_type == 2:
    string[i] = random.choice(uppercase_character_list)
  elif character_type == 3:
    string[i] = random.choice(number_list)
  else:
    string[i] = random.choice(symbol_list)

I have used this method at replit.com/@randomcat962/Practice-Typing-in-English#main.py and there was no issue.
Steps to reproduce:
Run the program and run the other program. See how one works and the other doesn’t?
I’m sorry if this is code error!

Bug appears at this link: https://replit.com/@randomcat962/Random-Passoword-Generator#main.py

Screenshot(s)/Screen Recording:

Browser/OS/Device: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Replit/1.0.0 Chrome/116.0.5845.97 Electron/26.1.0 Safari/537.36 ReplitDesktop

Replit Profile: https://replit.com/@randomcat962

Your error appears due to the fact that you are trying to get an item from a list with index 15 from a loop. In Python, the first element of the list has an index of 0, which means that if there are 15 elements in the list, the last element will have an index of 14.

Calling the range(15) function will return you a list with numbers from 0 to 15. But, as mentioned earlier, the string list has a maximum element index of 14. So it is necessary to specify 14, not 15, as the argument of the range function.

I also recommend that you use the join function to combine the list into a string. In your case, the union will look like this:

password = "".join(string) # an empty string is a string that will be inserted between all the elements of the string list

Corrected code:

import random

lowercase_character_list = ("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")
uppercase_character_list = ("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")
number_list = ("1", "2", "3", "4", "5", "6", "7", "8", "9", "0")
symbol_list = ("!", "@", "#", "$", "&", "?")

string = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "12", "13", "14", "15"]

for i in range(14):
  character_types = (1, 2, 3, 4)
  character_type = random.choice(character_types)

  if character_type == 1:
    string[i] = random.choice(lowercase_character_list)
  elif character_type == 2:
    string[i] = random.choice(uppercase_character_list)
  elif character_type == 3:
    string[i] = random.choice(number_list)
  else:
    string[i] = random.choice(symbol_list)

password = "".join(string)

print(password)

If this post was the answer to your question, you can mark it as a solution.

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.