Category: Software Design & Devlopment

Python Tables

tableI was asked in class how we can create a table in Python. There are a number of ways to do this but the easiest is probably to use the .format method.

The program reads in data from a CSV file before displaying the information in a two column table. There is a lot of internal commentary you can strip out leaving just the code.

The main part of the program is line 28. Where the contents of names and scores are displayed one line at a time with padding to make them appear to be in a table.

Read more

Higher – Python – Reading from a file

Python-logo-notext.svgA lot of people find reading from a sequential file difficult. Well lets break it down, first of all what is a sequential file? A sequential file is a text based (ASCII or UNICODE) file that is made up of both printing and non printing characters. The end of the file is signalled with EoF, often programmers put data in separate lines using a new line character. Sometimes the files are in CSV format, which means that each line holds a records worth of data.

The basic premise of any Python read file access is

  1. Open file for reading
  2. read each line
  3. close file

This post will mostly be focused on different ways of doing step 2.

Read more

National 5 – Boolean

A boolean variable is used to store True or False values. This can be very useful in programs, as you can use it in the program logic.

In the example below the user has to choose to exit the program, this sets the quitProg boolean variable to True which makes the result of the condition in line 3 False. Notice that True/False have capitals at the start of the value.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
quitProg=False

while not quitProg:

    #Program does stuff here
    
    choice=input('Do you want to quit (Y/N):')
    if choice in 'Yy':
        quitProg=True

print('Exiting program')    

N5 & Higher – Input Validation (Python)

Input validation is used in both National 5 and at Higher. It consists of 3 parts.

  1. Get the user to enter a value
  2. While the value is wrong
  3.     Get the user to enter a value

The use of the while construct allows the program to repeatedly ask the user to enter valid data.

1
2
3
4
5
6
7
8
9
age=0

#Input Validation
age=int(input('Please enter your age>'))
while age<0 or age>120: # Check that age is between 0 and 120
    print('Please check your age is between 0 and 120')
    age=int(input('Please enter your age>'))
    
print('Welcome')

This can also be used with strings, can you work out how to modify the code so that it would only take a ‘Yes’ ‘No’ answer?

Higher – Categories of High Level Languages

Computing Exit Pass

  • Mini Languages are langues designed for one purpose Ex HTML is used for web design
  • Domain Specific languages are also know as mini languages
  • Procedural languages are the most common type
  • Basic is a high level language designed to be easy to learn which became the popular language when computers became cheaper and popular in the 1970s
  • Procedural languages are the most common type of programming languages
  • Domain oriented languages focus on one task
  • A scripting language is basically a mini Languages
  • Prolog is a declarative logic language designed for artificial intelligence applications
  • Learned the different types of coding applications.

Are these statements valid… Discuss.