Your 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.
时间: 2024-01-21 11:05:12 浏览: 26
Sure, I can help you with that. Here's an example implementation in Python:
```python
def basic_math(operation, value1, value2):
if operation == '+':
return value1 + value2
elif operation == '-':
return value1 - value2
elif operation == '*':
return value1 * value2
elif operation == '/':
return value1 / value2
else:
return "Invalid operation"
```
This function takes in three arguments: the operation to perform as a string or character, and the two values to perform the operation on. It then checks which operation was requested using a series of if/elif statements, and returns the result of the operation. If the operation is not one of the four basic mathematical operations, it returns an error message.
Here's how you can use this function:
```python
>>> basic_math('+', 3, 5)
8
>>> basic_math('-', 6, 2)
4
>>> basic_math('*', 4, 7)
28
>>> basic_math('/', 10, 2)
5.0
>>> basic_math('%', 3, 4)
'Invalid operation'
```
I hope this helps! Let me know if you have any questions.