CS3101 Sample Coursework

Working in a group of 2 (or, exceptionally, a group of 3) the aim of this course work is to produce a program that satisfies a requirement for a system to be agreed with your tutor. You must agree the course work topic and list of functions by week 21. This must be uploaded to weblearn. Each group member must submit two files: 

 

1.The code listing, commented throughout, especially with the section headings showing theauthor of that section and its version history. The number of executable lines of code shouldbe approximately 100. (See the sample solution). 50% of the marks

 

2.A commentary on the part of the code written by the group member, together with adescription of how it was tested, and a reflective summary on the system as a whole andtheir contribution. 50% of the marks

 

EXAMPLE SYSTEM - Party List System 

The program partyList.py uses methods and a built in function to manipulate a list of the names of people invited to a party. The program uses menu to allow a user to: 

•add a name to a list of people to invite to a party,

•remove a name from a list of people invited to a party

•sort the names of people invited in alphabetical order

•display the names of those invited

•display the number of people invited

•quit the program.

 

CS3101 Sample Coursework Solution 

 

Party List System 

The program partyList.py uses methods and a built in function to manipulate a list of the names of people invited to a party. The program uses menu to allow a user to: 

•add a name to a list of people to invite to a party,

•remove a name from a list of people invited to a party

•sort the names of people invited in alphabetical order

•display the names of those invited

•display the number of people invited

•quit the program.

 

Code Fragments (listed by Group member – 50% of marks) 

 

# partyList.py 

# a program to introduce list methods 

# to maintain a list of names to invite 

# to a party. 

# COMMENTS MUST GIVE AUTHOR/VERSION HISTORY 

print "\t\tThis program allows you to maintain" 

print "\t\ta list of names of people to invite" 

print "\t\tto a party\n" 

names = [] # create an empty list 

choice = "z" # initialize choice with any value that will 

# set the while loop to true 

while choice != "q": 

print """ 

Choose what you would like to do from the following menu (or q to exit)" 

a: Add a name 

b: Remove a name 

c: Display the names invited 

d: Sort the names alphabetically 

e: Display the number of people invited 

q: Quit the program 

""" 

# Get user choice 

choice = raw_input("\nType in your choice(a,b,c,d,e (or q to exit): ") 

# add a new name

 

# AUTHOR 2nd GROUP MEMBER 

if choice == "a": 

newName = raw_input("\nEnter a name to add to the party list: ") 

names.append(newName) 

# remove a name 

elif choice == "b": 

oldName = raw_input("\nEnter the name you wish to remove: ") 

if oldName in names: 

names.remove(oldName) 

else: 

print 

print oldName,"is not in the party list." 

# display those invited 

elif choice == "c": 

print "\nYou have invited " 

for i in names: 

print i 

# sort the names alphabetically 

elif choice == "d": 

names.sort() 

# use the function len to calculate the number of people invited

 

# AUTHOR 3RD GROUP MEMBER 

elif choice == "e": 

numberOfPeople = len(names) 

if numberOfPeople == 0: 

print "\nYou have not invited anybody" 

elif numberOfPeople == 1: 

print "\nYou have invited", numberOfPeople,"person" 

else: 

print "\nYou have invited", numberOfPeople,"people" 

# quit the program or invalid input 

elif choice == "q": 

print "\nGoodbye!" 

else: # if user chose anything other than a – e, or q 

print "\n Sorry, there was an error in your input" 

print " Valid choices are a,b,c,d or e 

print " to quit: type q" 

 

Example documentation of code (50% of marks) 

 

Group member 1 

The first section, or fragment, of the program listing shown above contains the usual comments in the first five lines. The next three lines display on the screen the purpose of the program and are aimed at the end user. The next line creates an empty list, (nobody has, as yet, been invited by the program user to the party) and assigns this list to a variable that the programmer has chosen to label with the variable identifier names. This is just what we want at this point in this program as we have not yet given the user the choice of adding the names of people to invite to the party. 

The final line in the section of code above initialises a variable named choice to "z". This variable, choice will, later in the program, contain the user's choice from a menu that is displayed on the screen. The menu requests user input from the keyboard which is read into choice. The menu, and the remainder of the program is controlled by a while loop and for the while loop to execute at all the condition choice != "q" must be True initially. By initializing the variable choice to "z" before coding the while loop control structure, execution of the code inside the while loop is ensured to occur at least once. 

The first line of Menu code, while choice != "q": , sets up a while loop control structure and all of the remaining code in program forms the body of this structure. The body of the loop will execute at least once as discussed in the last paragraph. 

The above section, or fragment, of code displays the menu to the user, then reads in the user's choice using the raw_input function, then assigns the character that the user enters to the variable that the programmer has chosen to label choice. 

 

Group member 2 

If the user chooses "a", that is to add a new name to the party list, then the Boolean expression, choice = = "a" , evaluates to True and the block of code associated with the if block executes: 

The first line of code associated in the if block uses the raw_input function to prompt the user to enter a name to add which is then assigned to the variable newName. 

The next line of code: 

names.append(newName) 

adds the name entered by the user to the end of a list of names invited to the party. How it does it is by using a list method called append.. 

If the user chooses "b", that is to remove a name from the party list, then the Boolean expression choice = = "b" evaluates to True and the block of code associated with the first elif block in the program code executes: 

The first line of code associated in this elif block uses the raw_input function to prompt the user to enter a name to remove which is then assigned to the variable oldName. 

There then follows an if…else control structure that is contained, or nested, within the outer elif block. The purpose of this nested if…else control structure is to check if the name the user entered, which is stored in the variable labelled oldName, exists in the list variable names. If it exists the first occurrence of the name is removed using another list method called remove. If the name the user entered is not contained in the list the user is informed using a print statement. 

If the user chooses "c", that is display the list of names invited the party, then the Boolean expression choice = = "c" evaluates to True and the block of code associated with the second elif block in the program code executes: 

# display those invited 

elif choice == "c": 

print "\nYou have invited " 

for i in names: 

print i 

If the user chooses "d", that is sort the list of names invited the party alphabetically, then the Boolean expression choice = = "d" evaluates to True and the block of code associated with the fourth elif block in the program code executes: 

There is only one line of code associated with this fourth elif block. This uses another of the methods available for list processing, the sort method. 

When the user uses the menu to add names to the party list the names, contained in the list, are stored in the exact order that the user entered them. The user may wish to sort the order of the names in the list alphabetically before displaying them. 

 

Group member 3 

If the user chooses "e", that is display the number of people invited, then the Boolean expression choice = = "e" evaluates to True and the block of code associated with the fifth elif block in the program code executes: 

You can use the built in function len( ) to calculate the length of any sequence. You do this by using, or calling, the function with the sequence as an argument enclosed inside the parentheses, ( ). 

Here we use, or call, the function with len(names) as we wish to determine the length of the sequence of names contained in the list names. 

If the user chooses "q", then the Boolean expression choice = = "q" evaluates to True and the block of code associated with the sixth elif block in the program code executes and the program ends. 

If none of the valid options are chosen from the menu then the block of code associated with this else clause is executed. It uses print statements to inform the user of their error and restates the valid choices. 

The else clause is also part of the body of the while loop. As the user has not entered a q to exit the program the loop continues to iterate. 

 

ADDITIONAL SECTIONS 

Testing of each section 

An introduction, table of tests, and summary is expected here, by each group member for their section of code. 

Reflective Summary 

Each group member to write 3 or 4 paragraphs about the system and their contribution. This could include examples of particular problems they encountered and how they were solved

 

Need a custom answer at your budget?

This assignment has been answered 6 times in private sessions.

© 2024 Codify Tutor. All rights reserved