Ask Question
18 November, 02:30

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.

+4
Answers (1)
  1. 18 November, 02:33
    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 & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers