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

Answer

file = open("marks.txt", "a")
running=1
while (running ==1) :
    indexNo = raw_input("Enter Index No: ") 
    if (indexNo=="-1"):
        running=-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)
file.close()

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.
http://www.gliffy.com/


Project Draw is a free web-based diagram editor to create basic network, flowchart or quick form layout diagrams.
http://draw.labs.autodesk.com/ADDraw/draw.html


DIA is a desktop application on Windows and Linux to draw E-R diagrams, flowcharts.
http://live.gnome.org/Dia




MEANING OF A FLOWCHART


  • 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.

    Examples


    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.


Source
INTERACTIVE LEARNING OBJECTS: A FRAMEWORK BASED APPROACH:
Friedbert KasparUniversity of Applied ScienceFaculty of Computer Science

http://www.codewitz.net/papers/MMT_32-36_Friedbert_Kaspar.pdf

Monday, July 25, 2011

Python Recursion

#!/usr/bin/python

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

x=factorial(3)
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):
    x.append(i)
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 : ")
    b=int(a)
    print ("entered :" , b)
    c=roll()
    print ("received :" , c)
    if (c == b):
        print ("you win ")
        break
    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):
        a.append(i)
        print "appending", i
        print "now list", a
    return a

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

b=makelist()
printlist(b)



#Dictionaries

#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]

#
#