Syntax error when executing

1

I'm doing a calculator with Python. I started learning Python recently, so I do not know much about it. When I run the code, it gives me

  

"Syntax Error"

What is the solution? I keep giving the same error! ~ Here the code:

while True:

print('Opciones: ')
print ('Escribe "add" para sumar dos numeros')
print ('Escribe "subtract" para restar dos numeros')
print ('Escribe "multiply" para multiplicar dos numeros')
print ('Escribe "divide" para dividir dos numeros')
print ('Escribe "quit" para cerrar la calculadora')
user_input = input(": ")

if user_input == "quit":
 print ('Hasta la proxima')
break

elif user_input == "add":
 num1 = float(input("Escribe un numero: "))
 num2 = float(input("Escribe un numero: "))
 resultado = str(num1 + num2)
 print("La respuesta es " + resultado)

elif user_input == "subtract":
 num1 = float(input("Escribe un numero: "))
 num2 = float(input("Escribe un numero: "))
 resultado = str(num1 + num2)
 print("La respuesta es " + resultado)

elif user_input == "multiply":
 num1 = float(input("Escribe un numero: "))
 num2 = float(input("Escribe un numero: "))
 resultado = str(num1 + num2)
 print("La respuesta es " + resultado)

elif user_input == "divide":
 num1 = float(input("Escribe un numero: "))
 num2 = float(input("Escribe un numero: "))
 resultado = str(num1 + num2)
 print("La respuesta es " + resultado)

else:
 print("ERROR-Onbekende input")
    
asked by JuanVan12 21.11.2016 в 14:01
source

3 answers

2

As I read in the comments "That's the problem jajajja, it does not give me any error line." - JuanVan12 "When you run the program in a development environment it will usually tell you where the error is. I use Spyder and when executing the program it tells me the following:

It tells me that the error is on line 20, usually the error is higher than that line we must look at line 19.18 ... etc.

You can see the error sign that marks us in line 20. But the error is in 18 since the break is not necessary for this case since it is not being used for example a while cycle.

By eliminating the break, the warning is eliminated and therefore the error.

And if you run now the program runs without problems. Greetings, I hope I have helped you.

    
answered by 21.11.2016 / 15:11
source
2

You have already answered correctly, but since you are still having problems (see comments) I am going to put two examples of code working. If it still does not work well it is no longer a problem of the code:

As you have the code, as you have already commented, you just have to eliminate the break since it does not make sense to use it in this way, but to break a cycle. In this case it causes the elif not to have immediately before the if and a syntax error occurs. The code would look like this:

print('Opciones: ')
print ('Escribe "add" para sumar dos números')
print ('Escribe "subtract" para restar dos números')
print ('Escribe "multiply" para multiplicar dos números')
print ('Escribe "divide" para dividir dos números')
print ('Escribe "quit" para cerrar la calculadora')
user_input = input(": ")

if user_input == "quit":
    print ('Hasta la próxima!')

elif user_input == "add":
    num1 = float(input("Escribe un número: "))
    num2 = float(input("Escribe un número: "))
    resultado = str(num1 + num2)
    print("La respuesta es " + resultado)

elif user_input == "subtract":
    num1 = float(input("Escribe un número: "))
    num2 = float(input("Escribe un número: "))
    resultado = str(num1 - num2)
    print("La respuesta es " + resultado)

elif user_input == "multiply":
    num1 = float(input("Escribe un número: "))
    num2 = float(input("Escribe un número: "))
    resultado = str(num1 * num2)
    print("La respuesta es " + resultado)

elif user_input == "divide":
    num1 = float(input("Escribe un número: "))
    num2 = float(input("Escribe un número: "))
    resultado = str(num1 / num2)
    print("La respuesta es " + resultado)

If you want the program to do more than one operation per run, that is, you can be doing operations until you enter 'quit' you would use an infinite cycle and in this case if you would need the break to finish the execution:

while True:
    print('Opciones: ')
    print ('Escribe "add" para sumar dos números')
    print ('Escribe "subtract" para restar dos números')
    print ('Escribe "multiply" para multiplicar dos números')
    print ('Escribe "divide" para dividir dos números')
    print ('Escribe "quit" para cerrar la calculadora')
    user_input = input(": ")

    if user_input == "quit":
        print ('Hasta la próxima!')
        break

    elif user_input == "add":
        num1 = float(input("Escribe un número: "))
        num2 = float(input("Escribe un número: "))
        resultado = str(num1 + num2)
        print("La respuesta es " + resultado)

    elif user_input == "subtract":
        num1 = float(input("Escribe un número: "))
        num2 = float(input("Escribe un número: "))
        resultado = str(num1 - num2)
        print("La respuesta es " + resultado)

    elif user_input == "multiply":
        num1 = float(input("Escribe un número: "))
        num2 = float(input("Escribe un número: "))
        resultado = str(num1 * num2)
        print("La respuesta es " + resultado)

    elif user_input == "divide":
        num1 = float(input("Escribe un número: "))
        num2 = float(input("Escribe un número: "))
        resultado = str(num1 / num2)
        print("La respuesta es " + resultado)
    
answered by 21.11.2016 в 15:18
1

Your problem is in this line of code at the end of if :

if user_input == "quit":
    print ('Hasta la próxima!')
break // Aquí el break no es necesario ya que esto no es un condicional tipo switch, remueve este break y funcionará correctamente

Also the use of characters with accents can cause problems.

    
answered by 21.11.2016 в 14:09