Ask Question
21 October, 05:41

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.

+1
Answers (1)
  1. 21 October, 08:13
    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?
Find 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 and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers