1

The following is what I have done: Idea is to be able to calculate the result from 2 numbers depending whether the user wanted to add them, subtract, divide or multiply.

    print 'Welcome to my calculator program'
    Equation = raw_input('You want to add, subtract, divide or multiply? '
    firstno = raw_input ('Please enter first number ')
    secondno = raw_input('Please enter second number ')
    F1 = int(firstno)
    F2 = int(secondno)
    F3 = F1 + F2
    print F1, '+' ,F2, '=' ,F3

I am not actually responding to the user's input but rather assuming he would key in add. How can I code it such that it will react differently if the user want to subtract instead of add and so on? Tnks for help.

1
  • You're going to need if statement. Also it's a good idea to read the whole tutorial.
    – georg
    Oct 16, 2012 at 13:31

2 Answers 2

4

You could use a dictionary dispatch and some of the functions in the operator module as a base.

import operator as op

operations = {
    'add': {'func': op.add, 'char': '+'},
    'minus': {'func': op.sub, 'char': '-'}
}

Then lookup the keyword and apply the function and display the equation:

print F1, operations[Equation]['char'], F2, '=', operations[Equation]['func'](F1, F2)
1
  • I really love the operator module. It always turns out handy.
    – Bakuriu
    Oct 16, 2012 at 13:34
0

def start():

#main input variable to get a sign to do
calculator = input('What would you like to calculate? (x, /, +, -): ')
#gets 2 #'s to multiply, add, subtract, or divide 
if (calculator) == ('+'):
    add = input('what is the frist number would you like to add? ')
    addi = input('what is the second number would you like to add? ')
elif (calculator) ==('-'):
    sub = input('what is the first number would you like to subtract? ')
    subt = input('what is the second number you would like to subtract? ')
elif (calculator) == ('/'):
    div = input('what is the first number would you like to divide? ')
    divi = input('what is the second number would you like to divide? ')
elif (calculator) == ('x'):
    mult = input('what is the first number would you like to multiply? ')
    multi = input('what is the second number would you like to multiply? ')

#failsafe if done incorrect
elif (calculator) != ('x', '/', '-', '+'):
    print('try again')
    return


#adds 2 inputted #'s
if calculator == '+' :
    sumAdd = float (add) + float (addi)
    print(sumAdd)
#multiplies the 2 inputted #'s
elif calculator == 'x' :
    sumMul =  float (mult) * float (multi)
    print(sumMul)
#divides the 2 inputted #'s
elif calculator == '/' :
    sumDiv = float (div) / float (divi)
    print(sumDiv)
#subtracting the 2 inputted #'s
elif calculator == '-' :
    sumSub = float (sub) - float (subt)
    print(sumSub)

#returns to top of code to do another setup


return

start()

There is my calcuator Remember inputs on your +, -, *, / before jumping into numbers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.