Page 258 - CTS - CSA TP - Volume 2
P. 258
COMPUTER SOFTWARE APPLICATION - CITS
case 1 | 2:
return “Small positive number”
case 3 | 4 | 5:
return “Medium positive number”
case _ if num> 5:
return “Large positive number”
case _:
return “Negative number or other cases”
# Example usage
result = describe_number(num)
print(result)
Explanation:
• num = int(input(“Enter the Number: “)): This line prompts the user to enter a number, converts the input to an
integer (int), and stores it in the variable num.
• defdescribe_number(num):: This line defines a function named describe_number that takes a single argument
num.
• matchnum:: This is the beginning of a match statement, a new feature introduced in Python 3.10. It allows you
to perform pattern matching on the value of num.
• case 0:: This line checks if num is equal to 0.
• return “Zero”: If the value of num is 0, the function returns the string “Zero”.
• case 1 | 2:: This line checks if num is either 1 or 2.
• return “Small positive number”: If the value of num is 1 or 2, the function returns the string “Small positive
number”.
• case 3 | 4 | 5:: This line checks if num is either 3, 4, or 5.
• return “Medium positive number”: If the value of num is 3, 4, or 5, the function returns the string “Medium
positive number”.
• case _ if num> 5:: This line is a wildcard case that matches any value greater than 5.
• return “Large positive number”: If the value of num is greater than 5, the function returns the string “Large
positive number”.
• case _:: This line is another wildcard case that matches any other value.
• return “Negative number or other cases”: If the value of num doesn’t match any of the specific cases mentioned
earlier, the function returns the string “Negative number or other cases”.
• result = describe_number(num): This line calls the describe_number function with the user-inputted value num
and stores the result in the variable result.
• print(result): Finally, this line prints the result, which is the description of the entered number based on the
defined cases in the describe_number function.
Output:
243
CITS : IT & ITES - Computer Software Application - Exercise 125