The python print does not work for me

0

You will find the error at the end of the whole

Calculator

pregunta = raw_input("suma, resta, multiplicacion, division: ")

if pregunta == "suma": 
    valor1 = raw_input("Introduce el numero que quieres sumar ")
    valor2 = raw_input(str(valor1) + " " + "mas"  + ":" + " ")

def suma(valor1, valor2):
    operacion = str(valor1) + str(valor2)
    print ("=" + str(operacion))

Once opened the protrama everything goes well and the operation, in theory, is done but after that it closes directly without showing the result or giving an error.

    
asked by user 15.03.2018 в 07:27
source

1 answer

0

I have made a couple of changes which are:

  • Call the function of sum , which you do not do in your code (that's why you do not do the calculation).
  • Declare the function sum at the beginning and not at the end.
  • Treatment of exceptions.

Below you can see the corrected code, I hope it helps you:

def sumar(valor1, valor2):
    operacion = int(valor1 + valor2)
    print(str(valor1) +" + "+ str(valor2) + " = " + str(operacion))

try:
    pregunta = str(raw_input("suma, resta, multiplicacion, division: "))
    if(str.lower(pregunta) == "suma"):
        val1 = int(input("Introduce el numero que quieres sumar(1): "))
        val2 = int(input("Introduce el segundo numero que quieres sumar(2): "))
        sumar(val1, val2)
except:
    print("Error")
    
answered by 15.03.2018 в 08:58