09/04/2023
IGCSE
Edexcel Computer Scienceဖြေဆိုမည့် သားသားမီးမီးများလေ့လာစရာ
Pythonသင်ခန်းစာ
Paper2:Application of Computational Thinking
(Total for Question 6=20 marks)အတွက် လေ့လာစရာ Python
==========================================
driven program to calculate perimeter and area of different shapes
all the required functions with the calculations of perimeter of different shapes
def per_circle(radius):
perimeter = 2 * 3.14 * radius
print("Perimeter of Circle: ", perimeter)
def per_triangle(side1, side2, side3):
perimeter = side1 + side2 + side3
print("Perimeter of Triangle: ", perimeter)
def per_rectangle(height, width):
perimeter = 2 * (height + width)
print("Perimeter of Rectangle: ", perimeter)
def per_square(side):
perimeter = 4 * side
print("Perimeter of Square: ", perimeter)
all the required functions with the calculations of area of different shapes
def a_circle(radius):
area = 3.14 * radius * radius
print("Area of Circle: ", area)
def a_triangle(base, height):
area = base * height / 2
print("Area of Triangle: ", area)
def a_rectangle(height, width):
area = height * width
print("Area of Rectangle: ", area)
def a_square(side):
area = side * side
print("Area of Square: ", area)
of menu-driven approach
print("\nWELCOME TO MENSURATION PROGRAM! TRY CALCULATING PERIMETER AND AREA OF DIFFERENT GEOMETRIC SHAPES.")
the while loop to print menu list
while True:
print("\nMENU")
print("1. Circle")
print("2. Triangle")
print("3. Rectangle")
print("4. Square")
print("5. Exit")
shape_choice = int(input("\nEnter your choice of shape for calculations: "))
if shape_choice == 1:
while True:
print("\n1. Calculate perimeter of Circle")
print("2. Calculate area of Circle")
print("3. Exit")
choice1 = int(input("\nEnter choice for calculations: "))
the relevant method based on users choice using if-else loop
if choice1 == 1:
radius = int(input("Enter Radius of Circle: "))
per_circle(radius)
elif choice1 == 2:
radius = int(input("Enter Radius of Circle: "))
a_circle(radius)
elif choice1 == 3:
break
else:
print("Incorrect Choice!")
elif shape_choice == 2:
while True:
print("\n1. Calculate perimeter of Triangle")
print("2. Calculate area of Triangle")
print("3. Exit")
choice1 = int(input("\nEnter choice for calculations: "))
if choice1 == 1:
side1 = int(input("Enter length of side1: "))
side2 = int(input("Enter length of side2: "))
side3 = int(input("Enter length of side3: "))
per_triangle(side1,side2,side3)
elif choice1 == 2:
base = int(input("Enter base of traingle: "))
height = int(input("Enter height of traingle: "))
a_triangle(base, height)
elif choice1 == 3:
break
else:
print("Incorrect Choice!")
elif shape_choice == 3:
while True:
print("\n1. Calculate perimeter of Rectangle")
print("2. Calculate area of Rectangle")
print("3. Exit")
choice1 = int(input("\nEnter choice for calculations: "))
if choice1 == 1:
height = int(input("Enter height of rectangle: "))
width = int(input("Enter width of rectangle: "))
per_rectangle(height,width)
elif choice1 == 2:
height = int(input("Enter height of rectangle: "))
width = int(input("Enter width of rectangle: "))
a_rectangle(height,width)
elif choice1 == 3:
break
else:
print("Incorrect Choice!")
elif shape_choice == 4:
while True:
print("\n1. Calculate perimeter of Square")
print("2. Calculate area of Square")
print("3. Exit")
choice1 = int(input("\nEnter choice for calculations: "))
if choice1 == 1:
side = int(input("Enter side of square: "))
per_square(side)
elif choice1 == 2:
side = int(input("Enter side of square: "))
a_square(side)
elif choice1 == 3:
break
else:
print("Incorrect Choice!")
condition to get out of the while loop
elif shape_choice == 5:
print("Thank You! See you again.")
break
else:
print("Incorrect Choice. Please, try again.")
=====================================
# ------------------------------------------------------------
# Global variables
# ------------------------------------------------------------
classTable = []
# ------------------------------------------------------------
# Subprograms
# ------------------------------------------------------------
def showClass ():
if (len(classTable) != 0):
for student in classTable:
print (student)
else:
print ("----- Table is empty -----")
def loadData (pID, pLast, pFirst, pBirth, pBalance):
aRecord = []
aRecord.append (pID)
aRecord.append(pLast)
aRecord.append(pFirst)
aRecord.append(pBirth)
aRecord.append(pBalance)
# Append it to the class table
classTable.append (aRecord)
def insertRecord (pID, pLast, pFirst, pBirth, pBalance, pIndex):
aRecord = []
# Make a single student record
aRecord.append (pID)
aRecord.append(pLast)
aRecord.append(pFirst)
aRecord.append(pBirth)
aRecord.append(pBalance)
# Insert into the class table at index
classTable.insert (pIndex, aRecord)
def deleteRecord (pIndex):
del classTable[pIndex]
# -----------------------------------------------------------------
# Main program
# -----------------------------------------------------------------
showClass()
print ("\n... Appending three records ...")
loadData (384, "Collins", "Ivy", 2010, 15.34)
loadData (405, "Brown", "James", 2011, 18.87)
loadData (410, "Jones", "Karen", 2010, 12.98)
showClass()
print ("\n... Inserting a record at position [1] ...")
insertRecord (813, "Lee", "Charles", 2009, 5.47, 1)
showClass()
print ("\n... Deleting record at position [2] ...")
deleteRecord (2)
showClass()
=================================
# ------------------------------------------------------------
# Global variables
# ------------------------------------------------------------
classTable = []
# ------------------------------------------------------------
# Subprograms
# ------------------------------------------------------------
def showClass ():
if (len(classTable) != 0):
for student in classTable:
print (student)
else:
print ("----- Table is empty -----")
def loadData (pID, pLast, pFirst, pBirth, pBalance):
aRecord = []
aRecord.append (pID)
aRecord.append(pLast)
aRecord.append(pFirst)
aRecord.append(pBirth)
aRecord.append(pBalance)
# Append it to the class table
classTable.append (aRecord)
def insertRecord (pID, pLast, pFirst, pBirth, pBalance, pIndex):
aRecord = []
# Make a single student record
aRecord.append (pID)
aRecord.append(pLast)
aRecord.append(pFirst)
aRecord.append(pBirth)
aRecord.append(pBalance)
# Insert into the class table at index
classTable.insert (pIndex, aRecord)
def deleteRecord (pIndex):
del classTable[pIndex]
# -----------------------------------------------------------------
# Main program
# -----------------------------------------------------------------
showClass()
print ("\n... Appending three records ...")
loadData (384, "Collins", "Ivy", 2010, 15.34)
loadData (405, "Brown", "James", 2011, 18.87)
loadData (410, "Jones", "Karen", 2010, 12.98)
showClass()
print ("\n... Inserting a record at position [1] ...")
insertRecord (813, "Lee", "Charles", 2009, 5.47, 1)
showClass()
print ("\n... Deleting record at position [2] ...")
deleteRecord (2)
showClass()