-3

this is my first time doing a Python project so I'm a bit lost. I want to do that after the calculation, it does another one with the result from the previous calculation. How can I implement that?

def suma(valor1,valor2):
    resultado = valor1 + valor2
    return resultado

def resta(valor1,valor2):
    resultado = valor1 - valor2
    return resultado

def multiplicacion(valor1,valor2):
    resultado = valor1 * valor2
    return resultado

def division(valor1,valor2):
    resultado = valor1 / valor2
    return resultado

#Main Program
salir = False
while salir == False:
    operacion = input("""
    ¿Qué operación quieres hacer?
        -Suma
        -Resta
        -Multiplicación
        -División
        -Salir
    """)
    while operacion not in ("Suma","Resta","Multiplicación","Multiplicacion","División","Division","Salir"):
        operacion = input("""
    ¡Operación incorrecta! ¿Qué operación quieres hacer?
        -Suma
        -Resta
        -Multiplicación
        -División
        -Salir
    """)
    if operacion != "Salir":
        valor1 = input("¿Cúal es el primer valor de la operación? ")
        while valor1.isalpha() == True:
            valor1 = input("Valor incorrecto. Vuelve a introducir el primer valor de la operación. ")
        valor1 = float(valor1)

        valor2 = input("¿Cúal es el segundo valor de la operación? ")
        while valor2.isalpha == True:
            valor2 = input("Valor incorrecto. Vuelve a introducir el segundo valor de la operación. ")
        valor2 = float(valor2)

        if operacion == "Suma":
                print ("El resultado de", valor1, "más", valor2, "es igual a", suma(valor1,valor2))
        elif operacion == "Resta":
                print ("El resultado de", valor1, "menos", valor2, "es igual a", resta(valor1,valor2))
        elif operacion == "Multiplicación" or "Multiplicacion":
                print ("El resultado de", valor1, "por", valor2, "es igual a", multiplicacion(valor1,valor2))
        elif operacion == "División" or "Division":
            if valor2 == 0:
                valor2 = input("El resultado no se puede calcular, es infinito. Vuelve a introducir un segundo valor. ")
                while valor2.isnumeric == False:
                    valor2 = input("Valor incorrecto. Vuelve a segundo valor de la operación. ")
                valor2 = float(valor2)
                print ("El resultado de", valor1, "entre", valor2, "es igual a", division(valor1,valor2))
            else:
                print ("El resultado de", valor1, "entre", valor2, "es igual a", division(valor1,valor2))
        else:
            print ("Operación no disponible")
    else:
        salir = True

I tried to do a variables that stores the result, but I can't get it to work.

Alvaro
  • 3
  • 1
  • Welcome to Stack Overflow! This is a good opportunity for you to start familiarizing yourself with [using a debugger](https://stackoverflow.com/q/25385173/328193). When you step through the code in a debugger, which operation first produces an unexpected result? What were the values used in that operation? What was the result? What result was expected? Why? To learn more about this community and how we can help you, please start with the [tour] and read [ask] and its linked resources. – David Mar 07 '23 at 18:58
  • Please try to create a [mre], e.g. by having your calculator to do only sum and hardcoding the inputs. – Ignatius Reilly Mar 07 '23 at 19:16
  • Also, it's not clear how the value from the first operation is going to be used again. An example of the expected output would help. – Ignatius Reilly Mar 07 '23 at 19:17

1 Answers1

0

Assuming you mean you want the result of addition, subtraction etc to be stored you actually need to store the result not just print it. Then, on each loop decide if you want to use the value in resultado if it exists. I would recommend going back looking at your code more thoroughly and try to understand what the program is doing logically. You cant use a value you don't store.

        if operacion != "Salir":
        valor1 = input("¿Cúal es el primer valor de la operación? ")
        while valor1.isalpha() == True:
            valor1 = input("Valor incorrecto. Vuelve a introducir el primer valor de la operación. ")
        valor1 = float(valor1)

        valor2 = input("¿Cúal es el segundo valor de la operación? ")
        while valor2.isalpha == True:
            valor2 = input("Valor incorrecto. Vuelve a introducir el segundo valor de la operación. ")
        valor2 = float(valor2)

        if operacion == "Suma":
                resultado = suma(valor1,valor2) # Store the result
                print ("El resultado de", valor1, "más", valor2, "es igual a", resultado)
        elif operacion == "Resta":
                resultado = resta(valor1,valor2) # Store the result
                print ("El resultado de", valor1, "menos", valor2, "es igual a", resultado)
        elif operacion == "Multiplicación" or "Multiplicacion":
                resultado = multiplicacion(valor1,valor2) # Store the result
                print ("El resultado de", valor1, "por", valor2, "es igual a", resultado)
        elif operacion == "División" or "Division":
            if valor2 == 0:
                valor2 = input("El resultado no se puede calcular, es infinito. Vuelve a introducir un segundo valor. ")
                while valor2.isnumeric == False:
                    valor2 = input("Valor incorrecto. Vuelve a segundo valor de la operación. ")
                valor2 = float(valor2)
            resultado = division(valor1,valor2) # Store the result
            print ("El resultado de", valor1, "entre", valor2, "es igual a", resultado)

        else:
            print ("Operación no disponible")
    else:
        salir = True
Ellis Thompson
  • 408
  • 6
  • 18