Improve on this, I'm a beginner

#CALCULATOR.PY




def multiply(number1, number2):
    resultM = number1 * number2
    return resultM
#
def divide(number1, number2):
    resultD = number1 / number2
    return resultD
#
def subtract(number1, number2):
    resultS = number1 - number2
    return resultS
#
def addition(number1, number2):
    resultA = number1 + number2
    return resultA
#
operation = input('Which operation (*, /, -, +)')
number1 = float(input('What is the first number'))
number2 = float(input('What is the second number'))
#
if operation == '*':
    result = multiply(number1, number2)
#
if operation == '/':
    result = divide(number1, number2)
#
if operation == '-':
    result = subtract(number1, number2)
#
if operation == '+':
    result = addition(number1, number2)
#
print(f'You got {result}')

Functions with these effects are already provided for you in the standard library operator module. The way that you’ve written the rest of the code, it wouldn’t be helpful to have separate functions anyway; you could just write this logic directly. However, functions are useful for this purpose because, in Python, you can store them just like any other values, into a data structure, and pull them out later. That makes it easier to simplify the code for choosing the operation.

That can look like:

import operator

operations = {
    '+': operator.add, '*': operator.mul, '-': operator.sub,
    '/': operator.truediv
}

operation = input('Which operation (*, /, -, +)')
number1 = float(input('What is the first number'))
number2 = float(input('What is the second number'))

result = operations[operation](number1, number2)

For details, see these references:


Aside from that, you should consider what the program should do when the user’s input is not valid.

3 Likes

Your UI could use a space after the prompt to input.

Do you really want the program to silently end if the user enters some other string than *, /, + or -?

Good production calculators remember the last result, and allow the user to carry out further calculations. Really good ones let you press = to repeat the last operation.

Consider how you would automate testing of this program. Automated testing is powerful, and will help you uncover weird bugs you would never have thought of in a million years(*). To do so, I would suggest re-organising your code, so the calculator part is distinct from the input gathering part, and from the input validation part.

Further suggestions are mere nitpicking after what Karl said. Take that into account and it’s fine. But for bonus points:

Add type annotations.

If you might need to change the implementation of divide in future, prefix its name with __ (similarly for the others).

(*) 100% percent of the validating user inputs is a good idea.
99% of the time, for a numerical application, coercing a string to a float is good enough.
But personally, I think if accepting any of inf, -inf and NaN as valid inputs was intentional, then at the very least a comment mentioning that should be added.

py calc.py
Which operation (*, /, -, +)/
What is the first number1
What is the second numberinf
You got 0.0
1 Like

A pet peeve, but the names of 3 of those functions are verbs, whereas the fourth one is a noun.

3 Likes