Page 249 - CTS - CSA TP - Volume 2
P. 249

COMPUTER SOFTWARE APPLICATION - CITS




           TASK 6: Input and Output
           Code:
           # Example 4: User input and output
           user_input = input(“Enter your name: “)
           print(“Hello, “ + user_input + “!”)

           Explanation:
           In this example, we demonstrate how to take user input and display output based on that input.
           1  user_input = input(“Enter your name: “): This line prompts the user to enter their name. The input() function is
              used to receive input from the user, and the provided message (“Enter your name: “) serves as a prompt.

           2  print(“Hello, “ + user_input + “!”): This line prints a greeting using the user’s input. The + operator is used to
              concatenate the strings, and the exclamation mark is included for emphasis.
           Output:











           TASK 7: Output Formatting and Operators
           Code:
           # Example 5: Output formatting and comparison operators

           x = 10
           y = 15
           print(f”Is {x} equal to {y}? {x == y}”)
           print(f”Is {x} not equal to {y}? {x != y}”)
           Explanation:

           This example demonstrates output formatting and the use of comparison operators.
           1  x = 10 and y = 15: These lines assign values to the variables x and y.
           2  print(f”Is {x} equal to {y}? {x == y}”): This line uses an f-string to format the output. It checks whether x is equal
              to y using the equality operator (==) and prints the result.
           3  print(f”Is {x} not equal to {y}? {x != y}”): Similarly, this line checks whether x is not equal to y using the inequality
              operator (!=) and prints the result.
           Output:























                                                           234
                              CITS : IT & ITES - Computer Software Application - Exercise 123
   244   245   246   247   248   249   250   251   252   253   254