CodeTestValidate

No installations needed! Write Python directly in your browser and get instant feedback with our smart validation system.

Challenge # 1

Basic Calculator

Objective

Build a simple calculator that performs arithmetic operations based on a given operator.

Instructions

  • You are given three variables: a, b, and op.

  • a and b are numbers, and op is a string that represents an operator: '+', '-', '*', or '/'.

  • Perform the corresponding arithmetic operation between a and b.

  • Print the result of the operation.

  • If the operator is not recognized, print Invalid operator.

  • If the operation is division and b is 0, print Division by zero error.

Constraints:
Do not use input(). • `a` and `b` will be integers or floats. • `op` will be a string of length 1.
Exemple IO

Input: a = 10 b = 5 op = '+'

Output: 15

Input: a = 7 b = 3 op = '-'

Output: 4

Input: a = 6 b = 2 op = '/'

Output: 3.0

Input: a = 4 b = 0 op = '/'

Output: Division by zero error

Input: a = 4 b = 2 op = '^'

Output: Invalid operator

Try It Online!

Output:

Validate Your Results

Get your code reviewed and validated to understand what you did right and where you can improve.

Previous challengeFlatten a Nested ListNext challengeBubble Sort and Insertion Sort