Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def get_sum(num1, num2):
- return num1 + num2
- def subtract(num1, num2):
- return num1 - num2
- def divide(num1, num2):
- return num1 / num2
- def multiply(num1, num2):
- return num1 * num2
- def get_operation():
- operator_input = raw_input("Please enter a valid sign (+,-,/-*): ")
- switcher = {
- "+": get_sum,
- "-": subtract,
- "/": divide,
- "*": multiply
- }
- # operation func returns the appropriate function or "Invalid sign" string.
- operation_func = switcher.get(operator_input, "Invalid sign")
- if operation_func == "Invalid sign":
- return get_operation() # This is recursion meaning invoking a function from itself
- return operation_func
- def get_num(input_message):
- try:
- num = float(raw_input(input_message))
- except ValueError:
- return get_num("Please Enter a valid number: ")
- return num
- def calculate(operation, num1, num2):
- res = operation(num1, num2)
- # Checks if the calculated answer is an integer so it doesn't print with .0 at the end.
- if res % 1 == 0:
- return int(res)
- return float(res)
- first_num = get_num("Please enter the first number: ")
- second_num = get_num("Please enter the second number: ")
- result = calculate(get_operation(), first_num, second_num)
- print(result)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement