no puede convertir el objeto 'lista' a str implícitamente?
Frecuentes
Visto 2,377 veces
0
I have been making a program for me to study math problems. Noe everything works perfectly up to a point. The problem is displaying the problem. I made it so that based off the level you choose it uses certain numbers. I will give code related to the problem:
Lvlonenumbers =list(range(1, 51))
if Level == 'Lvl.1':
NumberList = Lvlonenumbers
NumberOne =random.choice(NumberList)
NumberTwo =random.choice(NumberList)
Answer =NumberOne + NumberTwo
print(str(NumberOne) + '+' + str(NumberTwo) + '=' + variable)
When the program goes to execute the print function it says cannot convert object 'list' to str implicitly. I do not understand why this happens. I have not seen this type of error before.
1 Respuestas
0
Variable is a list of strings. all the letters of the alphabet separated.
You can't add a list to a string, because Python doesn't know how to convert a list to string. This is your basic problem. You can covert it beforehand by using a join, like this: ''.join(variable)
:
lvl_one_numbers = list(range(1, 51))
if level == 'Lvl.1':
number_list = lvl_one_numbers
number_one = random.choice(lvl_one_numbers)
number_two = random.choice(lvl_one_numbers)
print('{0}+{1}={2}'.format(number_one, number_two, ''.join(variable))
I also changed your variable names as per the guía de estilo de Python.
Respondido el 11 de Septiembre de 13 a las 22:09
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas python python-3.x typeerror or haz tu propia pregunta.
Post the full traceback. Is
variable
a list as well? It probably is. - BlenderCreo que quieres decir
+ '=' + Answer
as you don't have avariable
name, and if you do, its probably a list. - Burhan KhalidVariable is a list of strings. all the letters of the alphabet separated. - pileoblock
You can't add a list to a string, because Python doesn't know how to convert a list to string. This is your basic problem. You can covert it beforehand by using a join, like this:
''.join(variable)
- Burhan Khalid