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)