0

This code gives me the expected output just fine:

1

>>> lista = ["8", "6"]
>>> lista = map(int, lista)
>>> print(list(lista))
'[8, 6]'

2

>>> lista = [8-2, 6-2]
>>> print(list(lista))
'[6, 4]'

But if I do something like this it doesn't work:

>>> lista = ["8-2", "6-2"]
>>> lista = map(int, lista)
>>> print(list(lista))

'print(list(lista))
       ^^^^^^^^^^^
ValueError: invalid literal for int() with base 10: '8-2''

Why is that? In my head it would go just like the 2º code :(
like this:

>>> lista = ["8-2", "6-2"]
>>> lista = map(int, lista)
>>> print(list(lista))
'[6,4]'
#not a real situation

I'm a beginner!!! What should I do to have the expected output? (sorry for bad English)

Michael Ruth
  • 2,938
  • 1
  • 20
  • 27
anio
  • 1
  • You need to parse the string expressions and perform the calculations in order to get the expected result. `8-2` is completely different from `"8-2"`. The former is an expression which consists of two integers and a binary operator on them, while the latter is a string literal. – Michael Ruth Dec 23 '22 at 21:31
  • You could look at this page for more information about Python Data Types:(https://www.w3schools.com/python/python_datatypes.asp ) – Desmanado Dec 23 '22 at 21:32

0 Answers0