First Program
I have also written a first Python program since the operating system comes pre-installed with Python. Rather than going with the default Hello World! program, I tried to challenge myself by writing a program which will check a user's PPS number - this is the Irish Personal Public Service Number which comprises 7 digits followed by a check character, which is derived from a mod23 checksum.I won't be getting a job programming the next generation of Space Shuttle, but at least it is a little more meaty than the pythonic Hello World:
print "Hello World"
There are probably better ways to implement this script, but I'm happy enough to pass it off as a first attempt to program on the RPi.
ppsn.py
# Initialisation of variables
sum = 0
letters = "WABCDEFGHIJKLMNOPQRSTUV"
# Set loop condition
done = False
# Loop until valid input is received
while not done:
ppsn = raw_input('Enter a PPSN to check: ')
# Check for correct length
if (len(ppsn) == 8):
# Check that first 7 characters are numbers
if ppsn[0:6].isdigit():
# Last character must be alphabetical
if ppsn[7].isalpha():
done = True
else:
print "The last character must be a letter"
else:
print "The first 6 characters must be numbers"
else:
print "The PPSN must be 8 characters in length, i.e. 7 digits & 1 letter"
# Checksum
for i in range(0,7):
num = int(ppsn[i]) # cast num to integer type
sum += num * (8-i)
# Get mod23 of sum to get position of check character
mod = sum % 23
# Correct for modulus equal to 0
if (mod == 0):
mod = 23
# Output results
if (ppsn[7] == letters[mod]):
print ppsn + " is a valid PPSN"
else:
print ppsn + " is not valid"
The output screenshot is from a SSH session into the RPi - the OpenBSD Secure Shell is disabled on startup by default, but can be easily enabled as follows:
sudo mv /boot/boot_enable_ssh.rc /boot/boot.rc
![]() |
| Output of first program written on RPi in Python |
Next steps:
Course related:- Re-install web server (apache)
- Re-install RDBMS (mysql)
- Install Ruby on Rails
- Git???
- Install OpenCV
- Get a webcam working, then get a second one working
- Write a dissertation on Stereo Image Object Extraction

No comments:
Post a Comment