Move python question files to 'questions' directory Move output files to 'outputs' directory Copy output files to 'questions' for ease of viewing Signed-off-by: Neil <neil@willofsteel.me>
35 lines
859 B
Plaintext
35 lines
859 B
Plaintext
Question 07:
|
|
Write a Python program to perform arithmetic calculation. The program should accept two operands and an operator, then display the result.
|
|
|
|
Answer:
|
|
def main(num1: float, num2: float, operator: str) -> None:
|
|
if operator == '+':
|
|
result = num1 + num2
|
|
elif operator == '-':
|
|
result = num1 - num2
|
|
elif operator == '*':
|
|
result = num1 * num2
|
|
elif operator == '/':
|
|
if num2 == 0:
|
|
print("Error: Division by zero")
|
|
raise ZeroDivisionError
|
|
result = num1 / num2
|
|
else:
|
|
print("Error: Invalid operator")
|
|
raise ValueError("Invalid operator. Use +, -, *, or /.")
|
|
|
|
print(f"Result: {result}")
|
|
|
|
if __name__ == '__main__':
|
|
main(10, 5, '+')
|
|
main(10, 5, '-')
|
|
main(10, 5, '*')
|
|
main(10, 5, '/')
|
|
|
|
|
|
Output:
|
|
Result: 15
|
|
Result: 5
|
|
Result: 50
|
|
Result: 2.0
|