Home > front end >  I want to concatenate characters in a list but i keep getting a type error
I want to concatenate characters in a list but i keep getting a type error

Time:08-20

I keep getting this error, I dont know where I went wrong. Please Help this is the error i keep getting:

Traceback (most recent call last):
  File "main.py", line 27, in <module>
    print('\nPASSOWRD: ', ''.join(letters_random))
TypeError: sequence item 3: expected str instance, int found

code:

import string
import random

letters = [p for p in string.ascii_letters]
numbers = [i for i in range(0,10)]
symbol = ['!', '@', '#', '$', '%', '&', '*', '(', ')']
letters_random = []
print('Welcome to the random Password Generator!\n')
user_input_letter = int(input("How many letters do you want for you password: "))
user_input_num = int(input("How many numbers do you want for you password: "))
user_input_symb = int(input("How many symbols do you want for you password: "))

for letter in range(0, user_input_letter):
  letters_random.append(random.choice(letters))

for num in range(0, user_input_num):
  letters_random.append(random.choice(numbers))

for letter in range(0, user_input_symb):
  letters_random.append(random.choice(symbol))

print('\nPASSOWRD: ', ''.join(letters_random))

CodePudding user response:

Change as per below, you cannot merge INT & STR

numbers = [str(i) for i in range(0,10)]

CodePudding user response:

join() requires a list of strings. However, you are building a list that has some strings and some integers. You should use digit characters instead of numbers.

CodePudding user response:

The .join() method expects string elements in the sequence it is iterating over. Your numbers list is a list of ints.

You should convert each int to a string as you append it:

for num in range(0, user_input_num):
  letters_random.append(str(random.choice(numbers)))
  • Related