9608 AS Computer Science Pre-release material Task 1

preview_player
Показать описание
Python implementation of Task 1.1 1.2 1.3 1.4. These tasks deal with 1D arrays. Task 1.1 deals with declaring and inputting data into an array. In this vidoe, you will see how to implement a search algorithm within an array (Task 1.3, 1.4)
Рекомендации по теме
Комментарии
Автор

#AS PRM21 P22 Python Code for Task 1.1, 1.2, 1.3, 1.4

import random

#Task 1.1 Populate array with studentName*Email*DOB

# DECLARE StudentInfo[0:30] : ARRAY OF STRING
# DECLARE CurrentName : STRING
# DECLARE CurrentEmail : STRING
# DECLARE CurrentDOB : STRING

print("This program will store Student Info in an array: ")

NumStudents = int(input("How many students? "))

StudentInfo = [""]*NumStudents #declare array of empty strings

for count in range(0, NumStudents):
CurrentName = input("StudentName: ")
CurrentEmail = input("Email: ")
CurrentDOB = input("Date of Birth: ")
CurrentLine =


print("Student Name \t\t Email \t\t DateOfBirth")
for count in range(0, NumStudents):
line = StudentInfo[count]
x=line.split("*")
print(count, x[0], "\t\t", x[1], "\t\t", x[2])

#Task 1.2 Deletes a StudentInfo line from the array and displays array

# DECLARE Deletelines : STRING
# DECLARE LineToDelete : INTEGER


Deletelines = input("Do you want to delete? Y or N: ")
while Deletelines=="Y":
LineToDelete = int(input("Which line to delete: "))
StudentInfo[LineToDelete] = ""
Deletelines = input("Do you want to delete Y or N: ")


for count in range(0, NumStudents):
if StudentInfo[count]!="":
print(StudentInfo[count])

#Task 1.3 - Search by name and output email address. (can be while/for)
#Recall that data is stored in StudentInfo array as Name*Email*DOB

Found = False
SearchItem = input("Type the name which you would like to find the email: ")
for count in range(0, NumStudents):
currentline =
if currentline[0]==SearchItem:
Found = True
print(currentline[1], "is the email corresponding to: ", SearchItem)

if Found==False:
print("That name was not found in the list. ")

#Task 1.4 Extend Task 1.4 to output all students who have bdays in a given month
# Define a fixed format for the date of birth, for example, "DDMMYY"
# Must iterate as a FOR loop

Found = False
SearchItem = input("Type the month to find all Students with bdays: ")
print("You are searching for: ", SearchItem)
for count in range(0, NumStudents):
currentline =
currentmonth = currentline[2]
if #works if format input is 11/11/2011
Found = True
print(currentline[0], "is a student with bday in the month: ", SearchItem)

if Found==False:
print("That month was not found in the list. ")

salmabanawan
Автор

Hi do you have VB codes for paper 22 and 42 please?

owtas
Автор

#Python Task 1.5 and Task 1.6

NumStudents = int(input("How many students? "))
# one way : StudentInfo = [[""]*5 ]*NumStudents
#another way to initialize
StudentInfo = [["" for j in range(5)]
for i in range(NumStudents)]
print(StudentInfo)

for i in range(0, NumStudents):
j=0
StudentInfo[i][j]=input("What is name: ")
is email: ")
is DOB: ")
his StudentID: ")
is TutorID: ")

for i in range(0, NumStudents):
for j in range(0, 5):
print(StudentInfo[i][j], end=' ')
print("\n")

#Task 1.6
searching = "yes"
while searching == "yes":

choice = input("What would you like to search for? Type name email dob studentid tutorid:")
while choice!="name" and choice!="email" and choice!="dob" and choice!="studentide" and choice!="tutorid":
choice = input("Error.What would you like to search for? Type name email dob studentid tutorid:")

if choice == "name":
searchitem = input("Which name would you like to see data for? ")
found = False
for i in range(NumStudents):
if
print(searchitem, "is in our list. Here are the details: \n")
found = True
for j in range(5):
print(StudentInfo[i][j], end=' ')
if found==False:
print("That student is not in our list.")

if choice == "email":
searchitem = input("Which email would you like to see data for? ")
found = False
for i in range(NumStudents):
if
print(searchitem, "is in our list. Here are the details: \n")
found = True
for j in range(5):
print(StudentInfo[i][j], end=' ')
if found==False:
print("That student is not in our list.")
if choice == "dob":
searchitem = input("Which dob would you like to see data for? ")
found = False
for i in range(NumStudents):
if
print(searchitem, "is in our list. Here are the details: \n")
found = True
for j in range(5):
print(StudentInfo[i][j], end=' ')
if found==False:
print("That student is not in our list.")

if choice == "studentid":
searchitem = input("Which studentid would you like to see data for? ")
found = False
for i in range(NumStudents):
if
print(searchitem, "is in our list. Here are the details: \n")
found = True
for j in range(5):
print(StudentInfo[i][j], end=' ')
if found==False:
print("That student is not in our list.")
if choice == "tutorid":
searchitem = input("Which tutorid would you like to see data for? ")
found = False
for i in range(NumStudents):
if
print(searchitem, "is in our list. Here are the details: \n")
found = True
for j in range(5):
print(StudentInfo[i][j], end=' ')
if found==False:
print("That student is not in our list.")
#code for search by dob and studentid and tutorid here. change StudentInfo[i][2] ..etc
searching = input("\n Would you like to continue searching for student data? yes or no ")

salmabanawan
Автор

# Task 2.1 Write student data to a file (Student ID e.g SA123 student email and dob 140303

# Task 2.2 Search by studentID and output the email address only.

def AddStudents(NumStudents):
#DECLARE StudentID, StudentEmail, StudentDOB, CurrentLine : STRING
#DECLARE count : INTEGER
f = open("StudentInfo.txt", "w")
for count in range(NumStudents):
StudentID = input("Type the student ID: ")
while len(StudentID)!=6:
StudentID = input("Error. Should be length 6: ")
StudentEmail = input("Type the email: ")
while len(StudentEmail) < 3 or (StudentEmail.find('@')==-1 and StudentEmail.find('.')==-1): #missing @and.
StudentEmail = input("Error. Email too short: ")
StudentDOB = input("Type the date of birth: ")
while len(StudentDOB)!=6:
StudentDOB = input("Type the date of birth: ")

CurrentLine =
f.write(CurrentLine)

f.close()
def AppendMoreStudents(): #this is a procedure
# DECLARE StudentID, StudentEmail, StudentDOB, CurrentLine : STRING
f2 = open("StudentInfo.txt", "a")
print("You will be prompted to type the ID, email and DOB of each student. Type ## to stop adding students: ")
StudentID = input("Type the StudentID")
while StudentID !="##":
StudentEmail = input("Type the student's email: ")
StudentDOB = input("Type their DOB: ")
CurrentLine =
f2.write(CurrentLine)
StudentID = input("Type the ID of the next student. Type ## to stop append more students: ")
f2.close()
def FindEmail():
# the code of this function which will read each line of the file and check if it is the required
# DECLARE StudentID, StudentEmail, StudentDOB, CurrentLine : STRING
# DECLARE Found: BOOLEAN

f1 = open("StudentInfo.txt", "r")
SearchValue = input("Type a student ID to search for the email: ")
Found = False
CurrentLine = f1.readline()
while (len(CurrentLine)!=0) and Found==False: #this code searches and stops at the first instance
StudentID = CurrentLine[0:6]
if StudentID == SearchValue:
StudentEmail = CurrentLine[6:-7]
Found = True
print("The student with ID", StudentID, "has the email: ", StudentEmail)
CurrentLine = f1.readline()

if Found==False:
print("StudentID not found in the file")
f1.close()

def
#DECLARE Found : BOOLEAN
#DECLARE CurrentLine:STRING
f3 = open("StudentInfo.txt", "r")
Found = False
CurrentLine = f3.readline()
while (len(CurrentLine)!=0): #this code will search the entire file for many instances
StudentID2Char = CurrentLine[0:2]
if StudentID2Char == IDLetters:
Found = True
print("This student has an ID starting with:", StudentID2Char, "and StudentID", CurrentLine[0:6])
CurrentLine = f3.readline()
if Found==False:
print("No Student IDs with these characters.")

# main program
print("Welcome to the Student Info Program. press 1 to create a new file, 2 to search that file, 3 to append: ")
print("Type 4 to search by the 2 letters of the ID")
choice = int(input())
if choice == 1:
NumStudents = int(input("How many students"))
AddStudents(NumStudents)
elif choice == 2:
FindEmail()
elif choice ==3:
AppendMoreStudents() #task 2.4
elif choice ==4:
IDLetters = input("What are the 2 letters you will search by? ")
while len(IDLetters)!=2:
IDLetters = input("Error. Type 2 letters to search: ")
SearchByIDLetters(IDLetters)
else:
print("Goodbye")

salmabanawan
Автор

Can you please explain AS and A levels PreRelease solution in Pseudocode? And in which website i can download prerelease material for AS and A levels?

roshmar