If I want to use your functions; it'd be very hard.
I'd have to modify the global scope of the program and have to know the quirks around choose
.
Always pass variables to functions as arguments.
def addition(choose, firstNum, secondNum):
if choose == 1:
print("Your result:", firstNum + secondNum)
Don't include choose
in your addition
and such functions.
It simply doesn't belong there and means all calls to addition have to be addition(1, lhs, rhs)
.
You've written the same print four times, you should really return the result of the operation and then write the print once.
def addition(lhs, rhs):
return lhs + rhs
def subtraction(lhs, rhs):
return lhs - rhs
def multiplication(lhs, rhs):
return lhs * rhs
def division(lhs, rhs):
return lhs / rhs
print("Welcome to the calculator,")
lhs = float(input("Give me first number: "))
rhs = float(input("Give me second number: "))
choose = int(input(
"Choose one option:\n"
"1.Addition\n"
"2.Subtraction\n"
"3.Multiplication\n"
"4.Division\n"
))
if choose == 1:
result = addition(lhs, rhs)
if choose == 2:
result = subtraction(lhs, rhs)
if choose == 3:
result = multiplication(lhs, rhs)
if choose == 4:
result = division(lhs, rhs)
print("Your result:", result)
We can further simplify the code by storing the options in a list or a dictionary.
A list is simpler to learn, but requires the options start at 0.
We can store a list ([``]
) of options in options
, seperating each option by a ,
.
We can then get an item from the list (1st being 0
, 2nd being 1
, ...) using [0]
.
>>> options = [addition, subtraction, ...]
>>> options[0](1, 2)
3
def addition(lhs, rhs):
return lhs + rhs
def subtraction(lhs, rhs):
return lhs - rhs
def multiplication(lhs, rhs):
return lhs * rhs
def division(lhs, rhs):
return lhs / rhs
options = [
addition,
subtraction,
multiplication,
division,
]
print("Welcome to the calculator,")
lhs = float(input("Give me first number: "))
rhs = float(input("Give me second number: "))
choose = int(input(
"Choose one option:\n"
"0.Addition\n"
"1.Subtraction\n"
"2.Multiplication\n"
"3.Division\n"
))
print("Your result:", options[choose](lhs, rhs))
Advanced changes
We can change the functions to lambdas.
These are function's without names.
options = [
lambda lhs, rhs: lhs + rhs,
lambda lhs, rhs: lhs - rhs,
lambda lhs, rhs: lhs * rhs,
lambda lhs, rhs: lhs / rhs,
]
By providing a name with each lambda we can build the options table.
We can by:
Change options
to a tuple with a name and a function.
options = [
("addition", lambda lhs, rhs: lhs + rhs),
...
]
Use enumerate
to go through options
getting the index at the same time as the name (and function).
for index, (name, _) in enumerate(options):
pass
Use an f-string to format the index and name into a string.
option = f"{index}.{name}\n"
Join all the available options together with str.join
.
options = [
("addition", lambda lhs, rhs: lhs + rhs),
("subtraction", lambda lhs, rhs: lhs - rhs),
("multiplication", lambda lhs, rhs: lhs * rhs),
("division", lambda lhs, rhs: lhs / rhs),
]
print("Welcome to the calculator,")
lhs = float(input("Give me first number: "))
rhs = float(input("Give me second number: "))
choose = int(input(
"Choose one option:\n"
+ "".join(
f"{index}.{name}\n"
for index, (name, _) in enumerate(options)
)
))
print("Your result:", options[choose][1](lhs, rhs))