Ask Question
25 December, 06:44

including how it can be stored and what types of operations we can perform. For example, we can write a program that squares numbers, but it wouldn't be able to square a word.

+3
Answers (1)
  1. 25 December, 09:36
    0
    The solution code is written in Python:

    def square (num) : if type (num).__name__ = = 'int': sq_num = num * num return sq_num else: return "Invalid input" print (square (5)) print (square ("Test"))

    Explanation:

    To ensure only certain type of operation can be applied on a input value, we can check the data type of the input value. For example, we define a function and name it as square which take one input number, num (Line 1).

    Before the num can be squared, it goes through a validation mechanism in by setting an if condition (Line 2) to check if the data type of the input number is an integer, int. If so, the num will only be squared otherwise it return an error message (Line 6).

    We can test our function by passing value of 5 and "Test" string. We will get program output:

    25

    Invalid input
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “including how it can be stored and what types of operations we can perform. For example, we can write a program that squares numbers, but ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers