Invalid syntax problem

Question:
The code shows an invalid syntax in line 40. How do I solve this? And is my code right? The purpose of the code is to generate a random password from the input of the user which is the day 5 project of the 100 days of code Angela Yu course.
Repl link:
https://replit.com/@Gabriel-jh0/password-generator-start?run=true#main.py

for num5 in range(0, int(len(symbols_needed)) - 1): #showing invalid sytax
  random_password.insert[random.randint(0, int(len(random_password)) - 1), symbols_needed[random_randint(0, int(len(symbols_needed)) -1 )]

use () instead of [] for .insert
so .insert[...].insert(...). Moreoever, you can get rid of int(len(...)) because len() is already an integer :slight_smile:

1 Like

Thanks. I fixed the brackets and parentheses. I also found that I accidentally mistyped random.randint. After running there was a problem with the code

random_password.append[letters_needed]

I changed it to

for num7 in letters_needed:
    random_password.append(num7)

I ran it again and I found that the output was missing two characters. I found that the problem was in the -1 in the range of

for num4 in range(0, len(numbers_needed) - 1):
  random_password.insert(random.randint(0, len(random_password) - 1), numbers_needed[random.randint(0, len(numbers_needed) -1 )])


for num5 in range(0, len(symbols_needed) - 1): 
  random_password.insert(random.randint(0, len(random_password) - 1), symbols_needed[random.randint(0, len(symbols_needed) -1 )])

I removed the -1 and I think I got the project right now.

Old version

#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(f"How many symbols would you like?\n"))
nr_numbers = int(input(f"How many numbers would you like?\n"))

#Eazy Level - Order not randomised:
#e.g. 4 letter, 2 symbol, 2 number = JduE&!91


#Hard Level - Order of characters randomised:
#e.g. 4 letter, 2 symbol, 2 number = g^2jk8&P
numbers_needed = []
letters_needed = []
symbols_needed = []
random_password = []
for num1 in range(0, int(nr_letters)):
  Password_key_letters= letters[random.randint(0, len(letters) - 1)]
  letters_needed.append(Password_key_letters)
  
for num2 in range(0, int(nr_numbers)):
  Password_key_numbers = numbers[random.randint(0, len(numbers) - 1)]
  numbers_needed.append(Password_key_numbers)

for num3 in range(0, int(nr_symbols)):
  Password_key_symbols = symbols[random.randint(0, len(symbols) -1)]
  symbols_needed.append(Password_key_symbols)

random_password.append[letters_needed]

for num4 in range(0, int(len(numbers_needed)) - 1 ):
  random_password.insert[random.randint(0, int(len(random_password)) - 1), numbers_needed[random_randint(0, int(len(numbers_needed)) -1 )]


for num5 in range(0, int(len(symbols_needed)) - 1): #showing invalid sytax
  random_password.insert[random.randint(0, int(len(random_password)) - 1), symbols_needed[random_randint(0, int(len(symbols_needed)) -1 )]

final_password = ''
for num6 in random_password:
  final_password += num6

print(f"Here is your password: {final_password}")

  Final Version

type or paste code here

#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(f"How many symbols would you like?\n"))
nr_numbers = int(input(f"How many numbers would you like?\n"))

#Eazy Level - Order not randomised:
#e.g. 4 letter, 2 symbol, 2 number = JduE&!91


#Hard Level - Order of characters randomised:
#e.g. 4 letter, 2 symbol, 2 number = g^2jk8&P
numbers_needed = []
letters_needed = []
symbols_needed = []
random_password = []
for num1 in range(0, int(nr_letters)):
  Password_key_letters= letters[random.randint(0, len(letters) - 1)]
  letters_needed.append(Password_key_letters)
  
for num2 in range(0, int(nr_numbers)):
  Password_key_numbers = numbers[random.randint(0, len(numbers) - 1)]
  numbers_needed.append(Password_key_numbers)

for num3 in range(0, int(nr_symbols)):
  Password_key_symbols = symbols[random.randint(0, len(symbols) -1)]
  symbols_needed.append(Password_key_symbols)

for num7 in letters_needed:
    random_password.append(num7)

for num4 in range(0, len(numbers_needed)):
  random_password.insert(random.randint(0, len(random_password) - 1), numbers_needed[random.randint(0, len(numbers_needed) -1 )])


for num5 in range(0, len(symbols_needed)): 
  random_password.insert(random.randint(0, len(random_password) - 1), symbols_needed[random.randint(0, len(symbols_needed) -1 )])

final_password = ''
for num6 in random_password:
  final_password += num6

print(f"Here is your password: {final_password}")

Hey @ramirezsanicask,
here’s how one would code your project using Python best practices:

"""Password Generator Project"""

import random
from string import ascii_letters as letters, digits as numbers

symbols = "!#$%&()*+"


def input_number(prompt):
  """Get a positive number from the user."""
  MAX = 32
  while True:
    try:
      x = int(input(prompt))
    except ValueError:
      print("Invalid number. Try again.")
      continue
    if x < 0:
      print("Number must be positive. Try again.")
      continue
    if x > MAX:
      print("Number too big. Try again.")
      continue
    return x


def generate_password(nr_letters, nr_numbers, nr_symbols):
  letters_needed = [random.choice(letters) for _ in range(nr_letters)]
  numbers_needed = [random.choice(numbers) for _ in range(nr_numbers)]
  symbols_needed = [random.choice(symbols) for _ in range(nr_symbols)]
  password = letters_needed + numbers_needed + symbols_needed
  random.shuffle(password)
  return "".join(password)


def main():
  print("Welcome to the PyPassword Generator!")
  while True:
    nr_letters= input_number("How many letters would you like in your password?\n")
    nr_numbers = input_number("How many numbers would you like?\n")
    nr_symbols = input_number("How many symbols would you like?\n")
    while True:
      password = generate_password(nr_letters, nr_numbers, nr_symbols)
      print(f"Here is your password: {password}")
      if input("Regenerate password? (y/n): ").strip().lower() != "y":
        break
    if input("Generate another password? (y/n): ").strip().lower() != "y":
      break


if __name__ == "__main__":
  main()
1 Like

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