filmov
tv
Python Coding Challenge - Easy #10 - Basic Mathematical Operations

Показать описание
Please like, share, subscribe for more and comment any questions
Try it yourself:
Source code :
# Our task is to create a function that does four basic mathematical operations.
# The function should take three arguments: operation(string/char), value1(number), value2(number).
# The function should return result of numbers after applying the chosen operation.
# Examples:
# Input: ("+", 4, 7) Output: 11
# Input: ("-", 15, 18) Output: -3
# Input: ("*", 5, 5) Output: 25
# Input: ("/", 49, 7) Output: 7
# Defining the function that will take an operation(string/char), value1(number), value2(number) as input.
# The function should return the result of the chosen operation.
def basic_math_operation(operation, value1, value2) :
# Use conditional statements (if - elif - else) to determine the chosen operation and perform calculation.
if operation == "+" :
result = value1 + value2
elif operation == "-" :
result = value1 - value2
elif operation == "*" :
result = value1 * value2
elif operation == "/" :
result = value1 / value2
# Return the result
return result
# Let's test the 'basic_math_operation' function with sample inputs.
def test_basic_math_operation() :
operation1 = "+"
value1_1 = 4
value2_1 = 7
result1 = basic_math_operation(operation1, value1_1, value2_1)
print(result1) # Output: 11
operation2 = "-"
value1_2 = 15
value2_2 = 18
result2 = basic_math_operation(operation2, value1_2, value2_2)
print(result2) # Output: -3
operation3 = "*"
value1_3 = 5
value2_3 = 5
result3 = basic_math_operation(operation3, value1_3, value2_3)
print(result3)
operation4 = "/"
value1_4 = 49
value2_4 = 7
result4 = basic_math_operation(operation4, value1_4, value2_4)
print(result4)
# Call the 'test_basic_math_operation' function
test_basic_math_operation()
# Summary:
# The 'basic_math_operation' function takes an operation (string/char), value1 (number), value2 (number) as input.
# It uses conditional statements (if-elif-else) to determine the chosen operation and performs the corresponding calculation.
# The function returns the result of the chosen operation as the output.
Try it yourself:
Source code :
# Our task is to create a function that does four basic mathematical operations.
# The function should take three arguments: operation(string/char), value1(number), value2(number).
# The function should return result of numbers after applying the chosen operation.
# Examples:
# Input: ("+", 4, 7) Output: 11
# Input: ("-", 15, 18) Output: -3
# Input: ("*", 5, 5) Output: 25
# Input: ("/", 49, 7) Output: 7
# Defining the function that will take an operation(string/char), value1(number), value2(number) as input.
# The function should return the result of the chosen operation.
def basic_math_operation(operation, value1, value2) :
# Use conditional statements (if - elif - else) to determine the chosen operation and perform calculation.
if operation == "+" :
result = value1 + value2
elif operation == "-" :
result = value1 - value2
elif operation == "*" :
result = value1 * value2
elif operation == "/" :
result = value1 / value2
# Return the result
return result
# Let's test the 'basic_math_operation' function with sample inputs.
def test_basic_math_operation() :
operation1 = "+"
value1_1 = 4
value2_1 = 7
result1 = basic_math_operation(operation1, value1_1, value2_1)
print(result1) # Output: 11
operation2 = "-"
value1_2 = 15
value2_2 = 18
result2 = basic_math_operation(operation2, value1_2, value2_2)
print(result2) # Output: -3
operation3 = "*"
value1_3 = 5
value2_3 = 5
result3 = basic_math_operation(operation3, value1_3, value2_3)
print(result3)
operation4 = "/"
value1_4 = 49
value2_4 = 7
result4 = basic_math_operation(operation4, value1_4, value2_4)
print(result4)
# Call the 'test_basic_math_operation' function
test_basic_math_operation()
# Summary:
# The 'basic_math_operation' function takes an operation (string/char), value1 (number), value2 (number) as input.
# It uses conditional statements (if-elif-else) to determine the chosen operation and performs the corresponding calculation.
# The function returns the result of the chosen operation as the output.