Tuesday, March 15, 2016

A/L ICT 2015 Python Code for Structured Question

Write a Python program to record the marks obtained by students at the term test. Each student has sat for the same three papers and each mark was given as an integer value out of 100 marks. Each student is identified by a unique index member which is also an integer. You should record the marks of student in a text file named 'marks.txt' in the following format.

 lndex_no_l ,mark_ll ,mark_l2,mark_l3
 Index_no_2 ,mark_2l ,mark_22 ,mark_23


file = open("marks.txt", "a")
while (running ==1) :
    indexNo = raw_input("Enter Index No: ") 
    if (indexNo=="-1"):
    else :
        mark1 = raw_input("Enter marks 1: ") 
        mark2 = raw_input("Enter marks 2: ") 
        mark3 = raw_input("Enter marks 3: ") 
        str=indexNo + "," + mark1 +  "," + mark2 +  ","  + mark2 + "\n"
        file.write (str)

Friday, September 30, 2011

Flow Charting Tools : Free and Open Source

The flowchart is a means of visually presenting the flow of data through an information processing systems, the operations performed within the system and the sequence in which they are performed. In this lesson, we shall concern ourselves with the program flowchart, which describes what operations (and in what sequence) are required to solve a given problem.

The program flowchart can be likened to the blueprint of a building. As we know a designer draws a blueprint before starting construction on a building. Similarly, a programmer prefers to draw a flowchart prior to writing a computer program.

American National Standard Flow Chart Symbols

Gliffy is a free web-based diagram editor to create, share, and collaborate on a wide range of diagrams.

Project Draw is a free web-based diagram editor to create basic network, flowchart or quick form layout diagrams.

DIA is a desktop application on Windows and Linux to draw E-R diagrams, flowcharts.


  • A flowchart is a diagrammatic representation that illustrates the sequence of operations to be performed to get the solution of a problem.
  • Flowcharts are generally drawn in the early stages of formulating computer solutions. Flowcharts facilitate communication between programmers and business people.
  • Flowcharts play a vital role in the programming of a problem and are quite helpful in understanding the logic of complicated and lengthy problems.
  • Once the flowchart is drawn, it becomes easy to write the program in any high level language. Often we see how flowcharts are helpful in explaining the program to others.
  • Flowchart is a must for the better documentation of a complex program.


    Source : eDrawSoft

Thursday, August 4, 2011

Step By Step Programing to Teach Python

Step By Step Programing to Learn Python

Used to explain syntax elements typically starts with a programming example. This example is then stepped through line by line which illustrates the evaluation of expressions.

Type 1.1
Input fix, Output fix, max. one step per line FindError locate error in source code

Type 2.1
1. Step: find erroneous line, 2. Step: Dialog with different possible explanations for the error, max.

Type 3.1
For a given output the user has to find the adequate initialization of a variable.

Type 3.2
User initializes Variable, finds the correct output from a collection of (five) answers

Type 3.3
A variable is initialized randomly and the user has to find the output from a List of answers

Type 3.4
A variable is initialized randomly and the user has to type in the output of the program

Type 3.5
Program is fixed, user gives output of the program in text input dialog

Type 3.6 Program is fixed, user gives output of the program in check box dialog,

Type 4.1
A line of Code is missing. The user has to point to where to insert the line. A list of 5 possibilities is given to chose from.

Type 4.2
A line of Code is missing. The user has to point to where to insert the line. The user has to type in the missing line.

Friedbert KasparUniversity of Applied ScienceFaculty of Computer Science


Monday, July 25, 2011

Python Recursion


def factorial(n):
    space = '*' * (4 * n)
    print space, 'factorial', n
    if n == 0:
        print space, 'returning 1'
        return 1
        recurse = factorial(n-1)
        result = n * recurse
        print space, 'returning', result
        return result

print x

Draw a flow chart for this program
What if we change x=factorial(3) to x=factorial(5)
Change the flow chart

Python Data Types Code Example - පයිතන් දත්ත වර්ග

x = []
for i in range(0,10):
print x
print "______________________________________"

y = x * 10

x = "Hello World!"

print x
print "______________________________________"
print y

# What are X and Y?


# Arithmetic
4 + 4 == 8
3 - 5 == -2
16 / 2 == 8
16 * 4 == 64
3 % 2 == 1 # Modulus

# Long Int Conversion - Useful for function type mismatches - Not needed here.
100000000000000000000L + long(100) == 100000000000000000100L


# Strings

a = "Hello"
b = "World!"

# Concatenation
a + " " + b == "Hello World!"

# Repetition and Concatenation
a * 3 + " " + b == "HelloHelloHello World!"
(a + " ") * 3 + b == "Hello Hello Hello World!"

# Indexing
b[2] == "r"

# Conversion of data to string - Sesame Street:
c = 7 # Backquote ` ` or str() operations
( a + " number " + `c` ) and ( a + " number " + str(c) ) == "Hello number 7"


Python dice roll code example

#dice roll function (6-sided by default)
import random

def roll(d=6):
     return random.randint(1, d)
while 1 > 0 :
    a=raw_input("enter number 1-6 : ")
    print ("entered :" , b)
    print ("received :" , c)
    if (c == b):
        print ("you win ")
    else :
        print ("you loose " , b , " <> ", c)
        print ("---------------------")

what does break do?
What is the use of random ?


Python Dictionaries and Lists Difference

#debugger example

print "This function creates a list."

def makelist():
    a = [] # list
    for i in range(1, 20):
        print "appending", i
        print "now list", a
    return a

def printlist(a):
    for i in a:
        print i,



#rock, paper, scissors game using dictionary

choice = raw_input("Enter s, r, or p: ")
results = {"s": "cut paper", "r": "crushes scissors", "p":  
    "covers rock"}
print choice, results[choice]