Read an operation

0

I want to make a program that reads an operation to me and performs it, for example I enter it:

Input
operación=input('Ingrese la operación')
  

For example, operation outside 4/2

 Output
 2

I try to do with an entry that takes it as a string and that it checks the sign and operates it as the sheel does when we enter this; my question is if there is a way to enter it in this way is operated effectively.

Thank you!

    
asked by DDR 17.03.2018 в 04:20
source

2 answers

1

The example I show you is helped by means of a function which will allow me to pass as an argument the requested operation, the same is to annex the input but I think with this example I show you how to control in a basic way that The user chooses which operation he wants to perform:

# -*- coding: utf-8 -*-
def calculadora(option):
    option1 = '+'
    option2 = '-'
    option3 = '*'
    option4 = '/'

    if(option == option1):
        print(1 + 2)
    elif(option == option2):
        print(1 - 2)
    elif(option == option3):
        print(1 * 2)
    elif(option == option4):
        print(1 / 1)
    else:
        print('No reconozco nada')

calculadora('*')

Update

In the following code instead of passing the numbers by default I declare two variables that can take any value; which I pass in the form of arguments at the end inside the parentheses of the function

# -*- coding: utf-8 -*-
def calculadora(option, numero1, numero2):
    option1 = '+'
    option2 = '-'
    option3 = '*'
    option4 = '/'

    if(option == option1):
        print(numero1 + numero2)
    elif(option == option2):
        print(numero1 - numero2)
    elif(option == option3):
        print(numero1 * numero2)
    elif(option == option4):
        print(numero1 / numero2)
    else:
        print('No reconozco nada')

calculadora('*', 2, 30)

Now if for example you want to control both the operation and the two numbers that are entered to make the operation, I show you the following example that uses the input method to collect the 3 values (of course you can still do more on the validation side but serve as an example)

# -*- coding: utf-8 -*-
def calculadora():
    option = input("Teclea la operacion: ") 
    numero1 = int(input("Teclea el numero1\n"))
    numero2 = int(input("Teclea el numero2\n"))
    option1 = '+'
    option2 = '-'
    option3 = '*'
    option4 = '/'

    if(option == option1):
        print(numero1 + numero2)
    elif(option == option2):
        print(numero1 - numero2)
    elif(option == option3):
        print(numero1 * numero2)
    elif(option == option4):
        print(numero1 / numero2)
    else:
        print('No reconozco nada')

calculadora()
    
answered by 17.03.2018 в 04:48
0

In your code should put the following, read the character that the user put and assuming that it is the sign of "/" should put it within a conditional.

signo="/"
if numero == signo:
    operacion = numero1 / numero2;
print(f"El resultado de la division es: {operacion}")

You should do it with each type of operation and if you wanted more than one number you would have to put it in an array and add, divide or subtract each number that is in the array.

Also in the case that you would like to put 4 numbers but suppose you want to do the following (1 + 2/3 * 4) the program should save the operation character in a similar way, in a few words, ask for a number and ask for an operation symbol.

    
answered by 17.03.2018 в 04:28