Syntax error problem in Else python

Asked

Viewed 787 times

0

I’m having a problem in this Python code where, when I try to run, it informs me that there is a syntax error in a else. Follow the code and error below:

n=int(raw_input(''))
i=0

for i in xrange(0,n):
 num_poco=int(raw_input(''))
 tipo_do_poco=raw_input('')
 custo_poco=float(raw_input(''))


 if tipo_do_poco.upper() == P or tipo_do_poco.upper() == G:
    vazao=float(raw_input(''))
    enxofre=float(raw_input(''))
    if enxofre == E:
      taxa_enxofre=float(raw_input(''))
    elif enxofre != E:
      taxa_enxofre=0.0
 else: 
    vazao=0.0
    taxa_enxofre=0.0
    lucro=0.0
    receita=0.0
 print'============================================'
 print('Empresa de Perfuracoes Furo Certo S/A.')
 print('Poco no.: %d'%(num_poco))
 print('Custo do Poco                : R$%11.2f'%(custo_poco))

 if tipo_do_poco == P:
    print('Volume de Petroleo encontrado: %11.2f'%(vazao))
    receita = vazao * 5.50 * (1 - taxa_enxofre)  
 elif tipo_do_poco == G:
    print('Volume de Gas      encontrado: %11.2f'%(vazao))
    receita = vazao * 2.20 * (1 - taxa_enxofre) 
 else:
    print('Volume de          encontrado: %11.2f'%(vazao))
    receita = 0.00
 lucro=receita-custo_poco


 if (receita - custo_poco)>50000.00:    
     print('Lucro do Poco (gusher)       : R$%11.2f\n'%(lucro)        
 else:
     print('Lucro do Poco                : R$%11.2f\n'%(lucro)   
     #print('============================================')
     #print('') 

 custo_total = (total_custo+custo_poco): 
 total_receita = (total_receita+receita): 
 total_lucro = (total_lucro+lucro):
print('=======================================')
print"Empresa de Perfuracoes Furo Certo S/A."
print('Total de Pocos          : %d'%(num_poco)
print('Total dos Custos        : R$%11.2f'%(total_custo)
print('Total da Receita        : R$%11.2f'%(total_receita)
print('Total do Lucro          : R$%11.2f'%(total_lucro)
print('=======================================')

Traceback (Most recent call last): File "python", line 41 Else:print('Poco profit : R$%11.2f n'%(profit) Syntaxerror: invalid syntax

  • 1

    Parentheses were missing from print of the latter else. And parentheses in the print of the latter if also

  • 3

    There are so many syntax errors that it is difficult to list them all. Parentheses on lines 40 and 42, 51, 52, 53 and 54; Two dots remaining on lines 46, 47 and 48; Function print with parentheses in Python version 2.7; very weird indentation for a Python script; and move on.

2 answers

3

Syntax errors just read the message that will know what is wrong: the error points to line 41 and if there is something wrong with that line, see the line above. In this case, one of the parentheses of line 40 had to be closed.

print('Lucro do Poco (gusher)       : R$%11.2f\n' % (lucro)) 
# (faltou este parenteses) --------------------------------^

Also, I take the opportunity to list other errors:

  1. The same error mentioned above in row 42;
  2. Two points remaining on lines 46, 47 and 48;
  3. Parentheses missing also on lines 51, 52, 53 and 54;
  4. In row 10, there are characters that have not been properly defined as strings:

    if tipo_do_poco.upper() == "P" or tipo_do_poco.upper() == "G":
    
  5. The same problem of 4 happens on lines 13, 15, 27 and 30;

  6. It makes no sense to define the variable enxofre as float on line 12 and then compare it with strings;
  7. The elif line 15 is completely unnecessary and can be replaced by a else;
  8. None raw_input has the identification for the user from which data is being read;
  9. Code indentation is completely wrong and unstable; always try to use 4 spaces for each indentation level;
  10. The variable total_custo is used in row 46, but is not defined in the code;
  11. The same for the variable total_receita on line 47;
  12. And for the variable total_lucro on line 48;
  13. In line 52 you display the variable total_custo, but only defines the custo_total;

Fix this and the program (probably) will run error-free - which does not mean it is correct.

  • 1

    I particularly liked the last sentence! lol

2

Arthur, when you come to use the % to mark where Voce wants the value of a certain variable within its print it must follow the following syntax:

print('quero falar dessa % variável ' %(variavel))

In your case the parenthesis was omitted at the end. That’s why it shows the syntax error.

Browser other questions tagged

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