What's the difference between these forms of command execution?

1

These commands in Python:

lista = eval('[' + input("Digite sua lista: ") + ']')

And this:

lista = input("Digite sua lista: ")

And this:

lista = [int(x) for x in input().strip()]

Why does the latter give the error below?

  

ValueError: invalid literal for int () with base 10: ','

    
asked by anonymous 29.05.2018 / 18:07

1 answer

1
  

list = eval ('[' + input ("Enter your list:") + ']')

Do not use eval() if you do not deeply master language and computing. In essence you do not need it.

  

list = input ("Enter your list:")

You are prompted to type something and save in the variable as a string , not a list.

  

list = [int (x) for x in input (). strip ()]

You are creating a list (a dynamic array array ). This is called expression understanding, so this loop is executed and the result of all looping, ie all steps, are saved in the list.

In this case you are picking up the generated string by typing the data ( input() ) and separating the words ( strip() ) into each integer ( int() ), waiting for the typed text separated by spaces to be numbers.

  

ValueError: invalid literal for int () with base 10: ','

You entered text that can not be converted to a number. I do not like this strategy, but in Python it's usually catching exception when this occurs to fix.

    
29.05.2018 / 18:18