invalid.syntax appears when the program is run

Asked

Viewed 99 times

0

I’ve tried to put palavra = '' and the same error keeps appearing:

lista = []
i = 0
while (palavra != 'Sair') or (palavra != 'sair'):
    i += 1
    palavra = str(input('digite alguma palavra: ')
    lista.append(palavra)
print(lista)
  • 2

    You’re missing a parenthesis in the line palavra = str(input('digite alguma palavra')), so the syntax error :)

  • Tip: instead of using the condition while (palavra != 'Sair') or (palavra != 'sair'), can use the lower(), that way: while (palavra.lower() != 'sair').

  • list = [] i = 0 word = 'a' while (word.Lower != 'quit'): i += 1 word = str(input('type some word: ')) list.append(word) print(list)

  • after modifications the program does not stop even after typing 'exit'.

  • thanks for the previous help

  • The parentheses of the lower(), see working on repl it.

  • would be able to do using the while?

  • would be able to do the program without using Lower(), or it is required when the contents of the list are strings?

  • please disregard the while question.

  • Just change your or by a and. I suggested the lower() because he will already take all the possibilities of the word sair, updated on replit if you want to see.

  • If you use the or will always be True , soon won’t leave the loop. I’ve added three ways around that with your code, on replit.

  • I am trying to scan the list with the variable "proc_word", but it does not work.

  • list = [] i = 0 word = ' while (word.Lower() != 'quit'): i += 1 word = str(input('type some word: ')) list.append(word) print(list) proc_word = int(input('type the word you are looking for: ') print(list[proc_word])

  • You saw the link to replit? I think we are well out of your problem. This mistake you are making is because you are declaring palavra and the entrance is going to proc_palavra and not palavra.

  • I am only working, at least for now, with the first form that you put. I don’t know the others

  • when I did an exercise to find a number in the list I was given the example position = int(input('type the position: ') print(list[position-1]) so I thought it would be enough to str instead of int to start looking for word instead of number

Show 11 more comments

2 answers

0

As you said, close the parentheses of str. But in fact, input already returns a string, so str is not necessary in this case.

Another thing is the condition of while. The or is true if any of the conditions is true (if only one of it is true, it is enough). And as long as the condition is true, the while continues to perform.

Therefore, (palavra != 'Sair') or (palavra != 'sair') you mean "if the word isn’t 'Out'' or if the word is not 'go out'". What happens then if the word is "go out"?

  • the first condition (palavra != 'Sair') is false because the word is equal to "Leave"
  • the second condition (palavra != 'sair') is true, because the word is not equal to "leave"

As one of the conditions is true, the result of the or is true, and so it does not leave the loop. To fix, just change or for and, for the and will only be true if both conditions are true (if one of them is false, it comes out of the while):

while (palavra != 'Sair') and (palavra != 'sair'):

Or use lower() to convert the string to lower case letters, then you only need to compare with "quit".

Another thing is that you don’t use the variable i for nothing, then you can take it too.

But there is still a detail. Seeing the code below (with the suggested modifications so far):

lista = []
palavra = ''
while palavra.lower() != 'sair':
    palavra = input('digite alguma palavra: ')
    lista.append(palavra)

print(lista)

The above code includes the word "exit" in the list, because within the while the word is read, added to the list and only then it is checked if it is equal to "exit".

If you do not want the word "exit" to be in the list, you should check this before entering it in the list. In this case you can take advantage and use break, interrupting the while:

lista = []
while True:
    palavra = input('digite alguma palavra: ')
    # se a palavra for "sair", sai do loop (e não coloca a palavra na lista)
    if palavra.lower() == 'sair':
        break
    lista.append(palavra)

print(lista)

But there’s another detail: if the word is SAIr, saIR or any other variation, it will also bring you out of the loop, since by calling lower(), all will be transformed into sair.

If you want to be more rigid and just interrupt the loop if you type exactly "Exit" or "Exit", you can do so:

lista = []
while True:
    palavra = input('digite alguma palavra: ')
    # se a palavra for "sair" ou "Sair", sai do loop (e não coloca a palavra na lista)
    if palavra in ('Sair', 'sair'):
        break
    lista.append(palavra)

print(lista)

It is worth remembering that to use lower() works for this case because I’m comparing to a string that only has ASCII characters (sair). But for a comparison involving other characters (used in other languages such as ß or fi) I suggest seeing this reply from Soen.

0

Missing closure of the input to string conversion parentheses on the following line:

palavra = str(input('digite alguma palavra: ')

Should be:

palavra = str(input('digite alguma palavra: '))
  • Thank you for the reply.

Browser other questions tagged

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