I/O and Modules in SAPL

Learn how to interact with users through input/output operations and leverage built-in modules

Learning Objectives

  • Understand how to read user input in SAPL programs
  • Learn to use input operations with loops for interactive programs
  • Explore SAPL's built-in math module for mathematical operations
  • Master string manipulation using the string module
  • Apply I/O operations and modules to solve practical programming problems

Introduction to I/O Operations

Input/Output (I/O) operations are essential for creating interactive programs that can communicate with users. In SAPL, I/O operations allow your programs to:

  • Read input from users through the keyboard
  • Display output to users on the screen
  • Create interactive applications that respond to user actions

You've already learned about the አሳይ() function for displaying output. In this tutorial, we'll focus on input operations and how to combine them with other SAPL features to create more powerful programs.

Reading User Input

SAPL provides several functions for reading different types of input from users:

Basic Input Functions

# Read a string (text)
ስም = read_string()

# Read an integer
እድሜ = read_int()

# Read a floating-point number
ክብደት = read_float()

# Read a boolean value
ተስማምተሃል = read_bool()
SAPL

Input with Prompts

# Prompt the user for their name
ስም = ጠይቅ("ስምህን አስገባ: ")
አሳይ("ሰላም, " + ስም + "!")

# Prompt for age and convert to integer
እድሜ_ፅሁፍ = ጠይቅ("እድሜህን አስገባ: ")
እድሜ = ቁጥር(እድሜ_ፅሁፍ)
አሳይ("ቀጣዩ ዓመት እድሜህ " + (እድሜ + 1) + " ይሆናል።")
SAPL

I/O Operations with Loops

Combining input operations with loops allows you to create interactive programs that can process multiple inputs. This is particularly useful for applications that need to collect a series of values or repeat until a certain condition is met.

Reading Multiple Inputs

Here's an example that reads 10 numbers from the user and calculates their sum:

# Calculate the sum of 10 numbers
ድምር = 0
ለ i በ ውስጥ(1, 11):
    አሳይ("ቁጥር " + i + " አስገባ: ")
    ቁጥር = read_int()
    ድምር = ድምር + ቁጥር

አሳይ("የ10 ቁጥሮች ድምር: " + ድምር)
SAPL

Reading Input Until a Condition

This example reads numbers from the user until they enter 0, then calculates the average:

# Calculate average of numbers until 0 is entered
ድምር = 0
ብዛት = 0
ቁጥር = -1  # Initial value to enter the loop

ሁኔታ ቁጥር != 0:
    ቁጥር = read_int()
    
    ቁጥር != 0 ከሆነ:  # Don't count the final 0
        ድምር = ድምር + ቁጥር
        ብዛት = ብዛት + 1

ብዛት > 0 ከሆነ:
    አማካይ = ድምር / ብዛት
    አሳይ("አማካይ: " + አማካይ)
ሌላ:
    አሳይ("ምንም ቁጥር አልገባም።")
SAPL

Example: Counter Program

Here's a simple counter program that increments a value and reads an integer from the user in each iteration:

ሂሳብ = 0

ሁኔታ ሂሳብ <= 10:
    ሂሳብ = ሂሳብ + 1
    አሳይ("ሂሳብ: " + ሂሳብ)
    አሳይ("አንድ ቁጥር አስገባ: ")
    x = read_int()
    አሳይ("አስገባኸው: " + x)
SAPL

Sample Output:

ሂሳብ: 1 አንድ ቁጥር አስገባ: 5 አስገባኸው: 5 ሂሳብ: 2 አንድ ቁጥር አስገባ: 10 አስገባኸው: 10 ...

Introduction to Modules

Modules are collections of related functions and variables that extend the capabilities of SAPL. They allow you to perform specialized operations without having to write all the code yourself. SAPL includes several built-in modules for common tasks:

  • Math Module: Functions for mathematical operations
  • String Module: Functions for string manipulation
  • File Module: Functions for reading from and writing to files
  • Random Module: Functions for generating random numbers

In this tutorial, we'll focus on the Math and String modules, which are the most commonly used.

Math Module

The Math module provides functions for performing mathematical operations beyond the basic arithmetic operators. These functions help you perform complex calculations more easily.

Common Math Functions

# Finding the maximum of two numbers
x = ከፍተኛ(1, 2)  # Returns 2
አሳይ("ከፍተኛ(1, 2): " + x)

# Finding the maximum of multiple numbers
y = ከፍተኛ(2, 3, 1, 5, 4)  # Returns 5
አሳይ("ከፍተኛ(2, 3, 1, 5, 4): " + y)

# Finding the minimum of two numbers
z = ዝቅተኛ(1, 2)  # Returns 1
አሳይ("ዝቅተኛ(1, 2): " + z)

# Absolute value
a = ፍጹም(-5)  # Returns 5
አሳይ("ፍጹም(-5): " + a)

# Power function
b = ኃይል(2, 3)  # Returns 8 (2^3)
አሳይ("ኃይል(2, 3): " + b)

# Square root
c = ስር(16)  # Returns 4
አሳይ("ስር(16): " + c)
SAPL

Example: Using Math Functions

Here's an example that uses math functions to find the maximum value and perform calculations:

x = ከፍተኛ(1, 2)
y = ከፍተኛ(2, 3)

አሳይ("x: " + x)
አሳይ("y: " + y)
አሳይ("x * y: " + (x * y))

# Calculate the hypotenuse of a right triangle
a = 3
b = 4
c = ስር(ኃይል(a, 2) + ኃይል(b, 2))
አሳይ("Hypotenuse: " + c)  # Should be 5
SAPL

Output:

x: 2 y: 3 x * y: 6 Hypotenuse: 5
FunctionDescriptionExampleResult
ከፍተኛ()Returns the maximum valueከፍተኛ(1, 2, 3)3
ዝቅተኛ()Returns the minimum valueዝቅተኛ(1, 2, 3)1
ፍጹም()Returns the absolute valueፍጹም(-5)5
ኃይል()Raises a number to a powerኃይል(2, 3)8
ስር()Returns the square rootስር(16)4

String Module

The String module provides functions for manipulating and analyzing text data. These functions help you work with strings more effectively.

Common String Functions

# Get the length of a string
ስም = "አበበ በሶ በላ"
ርዝመት = length(ስም)
አሳይ("የስም ርዝመት: " + ርዝመት)  # 11

# Convert to uppercase
ትልቅ = ወደ_ትልቅ(ስም)
አሳይ("ትልቅ ፊደል: " + ትልቅ)

# Convert to lowercase
ትንሽ = ወደ_ትንሽ(ስም)
አሳይ("ትንሽ ፊደል: " + ትንሽ)

# Check if a string contains a substring
ይዟል = ይዛል(ስም, "በሶ")
አሳይ("'በሶ' ይዟል: " + ይዟል)  # እውነት

# Replace text in a string
ተካ = ተካ(ስም, "በሶ", "ፍየል")
አሳይ("ተካ: " + ተካ)  # "አበበ ፍየል በላ"

# Split a string into parts
ክፍሎች = ክፈል(ስም, " ")
አሳይ("ክፍሎች: " + ክፍሎች)  # ["አበበ", "በሶ", "በላ"]
SAPL

Example: String Processing

Here's an example that uses string functions to process text:

x = "ሰማይ አረብ"
y = length(x)

አሳይ("ፅሁፍ: " + x)
አሳይ("ርዝመት: " + y)

# Count the number of words in a sentence
ሀረግ = "ይህ አ��ድ ረጅም ሀረግ ነው"
ቃላት = ክፈል(ሀረግ, " ")
የቃላት_ብዛት = length(ቃላት)
አሳይ("የቃላት ብዛት: " + የቃላት_ብዛት)  # 5
SAPL

Output:

ፅሁፍ: ሰማይ አረብ ርዝመት: 8 የቃላት ብዛት: 5
FunctionDescriptionExampleResult
length()Returns the length of a stringlength("አበበ")3
ወደ_ትልቅ()Converts to uppercaseወደ_ትልቅ("abc")"ABC"
ወደ_ትንሽ()Converts to lowercaseወደ_ትንሽ("ABC")"abc"
ይዛል()Checks if a string contains a substringይዛል("አበበ", "በ")እውነት
ተካ()Replaces text in a stringተካ("አበበ", "በ", "ለ")"አለለ"
ክፈል()Splits a string into partsክፈል("a b c", " ")["a", "b", "c"]

Combining I/O and Modules

The real power of I/O operations and modules comes when you combine them to create interactive programs that can process user input in sophisticated ways.

Example: Simple Calculator

This example creates a simple calculator that reads two numbers and an operator from the user, then performs the corresponding operation:

# Simple calculator
አሳይ("ቀላል ማስያ")
አሳይ("----------")

# Read the first number
አሳይ("የመጀመሪያ ቁጥር አስገባ: ")
ቁጥር1 = read_float()

# Read the operator
አሳይ("ኦፕሬተር አስገባ (+, -, *, /): ")
ኦፕሬተር = read_string()

# Read the second number
አሳይ("ሁለተኛ ቁጥር አስገባ: ")
ቁጥር2 = read_float()

# Perform the calculation
ውጤት = 0
ኦፕሬተር == "+" ከሆነ:
    ውጤት = ቁጥር1 + ቁጥር2
ኦፕሬተር == "-" ካልሆነ:
    ውጤት = ቁጥር1 - ቁጥር2
ኦፕሬተር == "*" ካልሆነ:
    ውጤት = ቁጥር1 * ቁጥር2
ኦፕሬተር == "/" ካልሆነ:
    ቁጥር2 != 0 ከሆነ:
        ውጤት = ቁጥር1 / ቁጥር2
    ሌላ:
        አሳይ("ስህተት: በ0 መካፈል አይቻልም")
        ውጤት = "ያልተገኘ"
ሌላ:
    አሳይ("ያልታወቀ ኦፕሬተር: " + ኦፕሬተር)
    ውጤት = "ያልተገኘ"

አሳይ(ቁጥር1 + " " + ኦፕሬተር + " " + ቁጥር2 + " = " + ውጤት)
SAPL

Example: Name Processor

This example reads a full name from the user and processes it using string functions:

# Name processor
አሳይ("ሙሉ ስምህን አስገባ: ")
ሙሉ_ስም = read_string()

# Split the name into parts
ክፍሎች = ክፈል(ሙሉ_ስም, " ")
የክፍሎች_ብዛት = length(ክፍሎች)

የክፍሎች_ብዛት >= 2 ከሆነ:
    የመጀመሪያ_ስም = ክፍሎች[0]
    የመጨረሻ_ስም = ክፍሎች[የክፍሎች_ብዛት - 1]
    
    አሳይ("የመጀመሪያ ስም: " + የመጀመሪያ_ስም)
    አሳይ("የመጨረሻ ስም: " + የመጨረሻ_ስም)
    አሳይ("የስም ፊደላት ብዛት: " + length(ሙሉ_ስም))
    አሳይ("ትልቅ ፊደል: " + ወደ_ትልቅ(ሙሉ_ስም))
ሌላ:
    አሳይ("እባክህ ሙሉ ስምህን አስገባ (የመጀመሪያ እና የመጨረሻ ስም)።")
SAPL

Practical Example: Data Entry Program

Let's create a more comprehensive example that combines I/O operations and modules to create a data entry program:

# Student data entry program
አሳይ("የተማሪ መረጃ መግቢያ ፕሮግራም")
አሳይ("------------------------")

# Initialize variables
ተማሪዎች = []
ውጤቶች = []
ቁጥር = 0

# Read student data
ሁኔታ እውነት:
    አሳይ("\nተማሪ " + (ቁጥር + 1))
    አሳይ("---------")
    
    # Read student name
    ስም = ጠይቅ("የተማሪ ስም (ለመውጣት 'exit' ይጻፉ): ")
    
    # Check if the user wants to exit
    ስም == "exit" ከሆነ:
        አቋርጥ
    
    # Read student scores
    ውጤት1 = ቁጥር(ጠይቅ("የመጀመሪያ ውጤት: "))
    ውጤት2 = ቁጥር(ጠይቅ("ሁለተኛ ውጤት: "))
    ውጤት3 = ቁጥር(ጠይቅ("ሶስተኛ ውጤት: "))
    
    # Calculate average score
    አማካይ = (ውጤት1 + ውጤት2 + ውጤት3) / 3
    
    # Add student to the list
    ተማሪዎች[ቁጥር] = ስም
    ውጤቶች[ቁጥር] = አማካይ
    
    ቁጥር = ቁጥር + 1

# Display results
አሳይ("\nውጤቶች")
አሳይ("-------")

ለ i በ ውስጥ(0, ቁጥር):
    አማካይ = ውጤቶች[i]
    
    # Determine grade
    ደረጃ = ""
    አማካይ >= 90 ከሆነ:
        ደረጃ = "A"
    አማካይ >= 80 እና አማካይ < 90 ካልሆነ:
        ደረጃ = "B"
    አማካይ >= 70 እና አማካይ < 80 ካልሆነ:
        ደረጃ = "C"
    አማካይ >= 60 እና አማካይ < 70 ካልሆነ:
        ደረጃ = "D"
    ሌላ:
        ደረጃ = "F"
    
    አሳይ(ተማሪዎች[i] + ": " + አማካይ + " (" + ደረጃ + ")")
SAPL

This program allows users to enter information for multiple students, calculates their average scores, and assigns grades based on those scores. It demonstrates how I/O operations and modules can be combined to create a useful application.

Best Practices

  • Provide clear instructions for input

    Always let users know what type of input is expected and in what format.

  • Validate user input

    Check that the input is valid before processing it to avoid errors.

  • Handle errors gracefully

    Anticipate potential errors (like division by zero) and handle them appropriately.

  • Use appropriate data types

    Convert input to the appropriate data type (string, integer, float) based on your needs.

  • Organize your code

    Group related functionality together and use comments to explain what your code is doing.

Summary

Key Concepts Learned

  • I/O operations allow your programs to interact with users through input and output
  • Functions like read_int(), read_string(), and ጠይቅ() are used to read input from users
  • Combining I/O operations with loops creates interactive programs that can process multiple inputs
  • Modules provide collections of related functions that extend SAPL's capabilities
  • The Math module includes functions for mathematical operations like ከፍተኛ(), ዝቅተኛ(), and ስር()
  • The String module provides functions for string manipulation like length(), ክፈል(), and ተካ()

I/O operations and modules are essential tools for creating interactive and powerful SAPL programs. By mastering these concepts, you can build applications that can communicate with users, process their input, and perform complex operations with ease.