Library Routines
What are library routines?
A library routine is reusable code that can be used throughout a program
The code has been made public through reusable modules or functions
Using library routines saves programmers time by using working code that has already been tested
Some examples of pre-existing libraries are:
Random
The random library is a library of code which allows users to make use of 'random' in their programs
Examples of random that are most common are:
Random choices
Random numbers
Random number generation is a programming concept that involves a computer generating a random number to be used within a program to add an element of unpredictability
Examples of where this concept could be used include:
Concept | Pseudocode | Python |
|---|
Random numbers | RANDOM(1,6)
| import random
number = random.randint(1,10)
number = random.randint(-1.0,10.0)
|
Random choice | | import random
choice = random.choice()
|
Examples
Pseudocode | Python |
|---|
// (In pseudocode we just use the RANDOM() function, so no import is needed)
// Ask user to enter a username and password
OUTPUT "Enter a username: "
INPUT User
OUTPUT "Enter a password: "
INPUT Pw
// Check if the user and password are correct
IF User = "admin" AND Pw = "1234" THEN
// Generate a random 4-digit code
Code ← ROUND((RANDOM() * 9000) + 1000, 0)
// Print the code
OUTPUT "Your code is ", Code
ELSE
OUTPUT "Invalid username or password."
ENDIF
| # importing random library
import random
# asking user to enter a username and password
user = input("Enter a username: ")
pw = input("Enter a password: ")
# checking if the user and password are correct
if user == "admin" and pw == "1234":
# generating a random 4-digit code
code = random.randint(1000, 9999)
# printing the code
print("Your code is", code)
else:
print("Invalid username or password.")
|
Pseudocode | Python |
|---|
// Create a list of numbers for the national lottery
DECLARE LotteryNumbers : ARRAY[1:49] OF INTEGER
FOR Index ← 1 TO 49
LotteryNumbers[Index] ← Index
NEXT Index
// Create an empty list to store the chosen numbers
DECLARE ChosenNumbers : ARRAY[1:6] OF INTEGER
// Loop to pick 6 numbers from the list
FOR Count ← 1 TO 6
// Use RANDOM to pick a number from the remaining list
RandomIndex ← ROUND((RANDOM() * (50 - Count)), 0)
IF RandomIndex = 0 THEN
RandomIndex ← 1
ENDIF
Number ← LotteryNumbers[RandomIndex]
// Add the chosen number to the list of chosen numbers
ChosenNumbers[Count] ← Number
// Remove the chosen number from the list of available numbers
FOR i ← RandomIndex TO 48
LotteryNumbers[i] ← LotteryNumbers[i + 1]
NEXT i
NEXT Count
// Sort the chosen numbers in ascending order
FOR i ← 1 TO 5
FOR j ← i + 1 TO 6
IF ChosenNumbers[i] > ChosenNumbers[j] THEN
Temp ← ChosenNumbers[i]
ChosenNumbers[i] ← ChosenNumbers[j]
ChosenNumbers[j] ← Temp
ENDIF
NEXT j
NEXT i
// Output the chosen numbers
OUTPUT "The winning numbers are: "
FOR i ← 1 TO 6
OUTPUT ChosenNumbers[i]
NEXT i
| import random
# Create a list of numbers for the national lottery
lottery_numbers = list(range(1, 50))
# Create an empty list to store the chosen numbers
chosen_numbers = []
# Loop to pick 6 numbers from the list
for _ in range(6):
# Use random.choice to pick a number from the list
number = random.choice(lottery_numbers)
# Add the chosen number to the list of chosen numbers
chosen_numbers.append(number)
# Remove the chosen number from the list of available numbers
lottery_numbers.remove(number)
# Sort the chosen numbers in ascending order
chosen_numbers.sort()
# Output the chosen numbers
print("The winning numbers are:", chosen_numbers)
|
Round
The round library is a library of code which allows users to round numerical values to a set amount of decimal places
An example of rounding is using REAL values and rounding to 2 decimal places
The round library can be written as:
ROUND(<identifier>, <places>)
Concept | Pseudocode | Python |
|---|
Round | Cost ← 1.9556
OUTPUT ROUND(Cost,2)
# Outputs 1.96
| number = 3.14159 rounded_number = round(number, 2)
print(rounded_number)
# Outputs 3.14
|
Example
// Example using the ROUND function
DECLARE Price : REAL
DECLARE RoundedPrice : REAL
Price ← 12.6789
// Round the value of Price to 2 decimal places
RoundedPrice ← ROUND(Price, 2)
OUTPUT "The rounded price is ", RoundedPrice
So if the variable Price was 12.6789, the output would be:
The rounded price is 12.68