Python code error - Syntaxerror: can’t assign to Function call

Asked

Viewed 2,327 times

1

I’m doing a futsal game with POO, and I went to do a function outside the class and in the global scope assigns the vector lista_team = [], only when I’m trying to run the stage where the code is, it generates an error, what can it be? I want to add the answer to input to the vector, at each position.

lista_team.append() = input('Digite o nome do seu time: \n')
File "C:\Python37\Scripts\futsal.py", line 45
    lista_team.append() = input('Digite o nome do seu time: \n')
    ^
SyntaxError: can't assign to function call
  • 2

    Just pass on the result of input as a parameter of append: lista_team.append(input('Digite o nome do seu time: \n'))

2 answers

1

When you use the equal sign Python understands that you are trying to declare a variable, and as you may already know, "append" is a method of the "list" object".

It works like this:

lista_team = []
lista_team.append(input('digite o nome do seu time!\n'))
  • Missing the input function

  • I get it, thank you !

0

Vc can declare the input and after entering the result in the list.

lista_team = []
name_team = input('digite o nome do time')
lista_team.append(name_team)

I prefer this way because you can add a validation layer simply.

lista_team = []
name_team = input('digite o nome do time')
if <sua_verificção>:
    lista_team.append(name_team)
  • I get it, thank you !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.