Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Print a Simple Python Result

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Print a Simple Python Result. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

A small utility in Reply's machine-learning tooling must display the result of one basic calculation consistently. Implement a function that evaluates two numbers with a supplied operator and returns a formatted string suitable for printing.

Formal Specification

Implement format_calculation(a, b, operator), where a and b are numbers, and operator is one of +, -, *, or /. Return a string in the exact format a operator b = result. For division, assume b is nonzero. Use Python's normal arithmetic rules, including decimal division with /. The input will always contain a supported operator.

Do not print inside the function. Returning the string keeps the function easy to test, and a caller can print the returned value directly.

Constraints

  • a and b are integers or floating-point numbers
  • operator is one of +, -, *, /
  • b is nonzero when the operator is /
  • The result fits in Python's numeric types

Function Signature

def format_calculation(a, b, operator):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output