首页 > > 详细

调试Python、Python编程解析、辅导Python编程、Python语言讲解留学生

Problem Solving and Programming
INTRODUCTION
This document describes the second assignment for Problem Solving and Programming.
The assignment is intended to provide you with the opportunity to put into practice what you have learnt in the course
by applying your knowledge and skills to the implementation of a Python module that contains a variety of list related
functions and a game of Blackjack that uses classes, objects and functions.
This assignment is an individual task that will require an individual submission. You will be required to submit
your work via Moodle before Friday Week 12, 11:55pm. You will also be required to present your work to your
practical supervisor during your practical session held in week 12 of the study period. Important: You must
attend the practical session that you have been attending all study period in order to have your assignment marked.
This document is a specification of the required programs and their output. Please ask your practical supervisor if you
do not understand any part of this document or the assignment.
Part I: List Module
You are required to implement a Python module that contains functions that manipulate lists. Please ensure that you
read sections titled 'Part I specification' below for further details.

Part II: Blackjack
You are required to write a game of Blackjack using methods and objects created from provided classes. When a
game is complete, highscores will need to be recorded in a text file. Please ensure that you read sections titled 'Part II
specification' below for further details.
Please ensure that you read sections titled ‘Part I Specification’ and ‘Part II Specification’ below for further
details.


PART I SPECIFICATION – LIST MODULE
You are required to write a list_function.py module (containing only the functions listed below). This file is
provided for you however, you will need to modify this file by writing code that implements the functions listed below.
Please read the slides on modules available on the course website if you would like more information on
modules.
You are required to implement a Python module containing the following functions:

 length()
 to_string()
 count()
 find()
 insert_value()
 remove_value()

Implement one function at a time.

Place the code that implements each function in the appropriate place in the list_function.py file.

For example, if you were implementing the length() function, you would place the code that calculates and returns
the length of the list under the comment ‘Place your code here’ (within the length function definition) seen below.

# Function length() – place your own comments here… : )
def length(my_list):

# This line will eventually be removed - used for development purposes only.
print("In function length()")

# Place your code here

Test your function by running the list_function_test.py to ensure each function is working correctly before
starting on the next function. The list_function_test.py file is a test file that contains code that calls the
functions contained in the list_function.py module. Please do not modify the test file. You must test your
functions with this file to ensure that they are working correctly.

Compare your output with the section titled 'Sample Output – Part I' to ensure that your function is working as
it should.
PSP Assignment 2 - 201801 Page 5 of 24

REQUIREMENTS

It is expected that your solution will include the use of:

 The supplied list_function.py module (containing the functions listed below). This is provided for you –
you will need to modify this file.
 Functions (length, to_string, count, find, insert_value and remove_value) implemented
adhering to the assignment specifications.
 The supplied list_function_test.py file. This is provided for you – please DO NOT modify this file.
 Well constructed while loops. (Marks will be lost if you use break statements in order to exit from loops).
 Well constructed for loops. (Marks will be lost if you use break statements in order to exit from loops).
 Appropriate if/elif/else statements of the style. used in the class notes.
 Output that strictly adheres to the assignment specifications.
 Good programming practice:
o Consistent commenting and code layout. You are to provide comments to describe: your details, program
description, all variable definitions, all functions, and every significant section of code.
o Meaningful variable names.

 Your solutions MAY make use of the following:
o Built-in functions range() and str().
o List method append() to create/build new lists. i.e. list_name.append(item).
o Concatenation (+) operator to create/build new strings.
o Comparison operators (==, !=, , etc).
o Access the individual elements in a list with an index (one element only). i.e. list_name[index].
o Use of any of the functions you have written as part of the assignment. i.e. length() function.

 Your solutions MUST NOT use:
o Built-in functions (other than range() and str() functions).
o Slice expressions to select a range of elements from a list. i.e. list_name[start:end].
o List methods (other than the append() method. i.e. list_name.append(item)).
o String methods.
o break, or continue statements
o quit() or exit()

Please ensure that you use Python 3.4 or later in order to complete your assignments. Your programs MUST run
using Python 3.4.
PSP Assignment 2 - 201801 Page 6 of 24

STAGES
It is recommended that you develop this part of the assignment in the suggested stages. Many problems in later
stages are due to errors in early stages. Make sure you have finished and thoroughly tested each stage before
continuing.
The following stages of development are recommended:
Stage 1
You will need both the list_function.py and list_function_test.py files for this assignment. These have
been provided for you. Please download both of these files from the course website and ensure that they are in
the same directory as each other.

Test to ensure that this is working correctly by opening and running the list_function_test.py file. If this is
working correctly, you should now see the following output in the Python shell when you run your program:

Stage 3
Write a function called to_string(my_list, sep=', ') that takes a list and a separator value as parameters
and returns the string representation of the list (separated by the separator value) in the following form.

item1, item2, item3, item4

The separator value must be a default argument. i.e. sep=', '

You must use a loop in your solution. You must not use built-in functions (other than the range() and str()
functions), slice expressions, list methods or string methods in your solution. You may use the concatenation (+)
operator to build the string. You must return a string from this function.

Stage 4
Write a function called count(my_list, value) that takes a list and a value as parameters. The function
searches for the value in the list and returns how many times the value appears in the list. You may assume that the
elements of the list can be compared using the comparison perators ==, !=, etc. You must use a loop in your solution.
You must not use built-in functions (other than the range() function), list methods or string methods in your
solution.

Stage 5
Write a function called find(my_list, value) that takes a list, and a value as parameters. The function searches
for the value in the list and returns the index at which the first occurrence of value is found in the list. The function
returns -1 if the value is not found in the list.

Stage 6
Write a function called insert_value(my_list, value, insert_position) that takes a list, a value and an
insert_position as parameters. The function returns a copy of the list with the value inserted into the list (my_list)
at the index specified by insert_position. Check for the insert_position value exceeding the list (my_list)
bounds. If the insert_position is greater than the length of the list, insert the value at the end of the list. If the
insert_position is less than or equal to zero, insert the value at the start of the list. You must use a loop(s) in your
solution. You may make use of the list_name.append(item) method in order to build the new list. You must not
use built-in functions (other than the range() function), slice expressions, list methods (other than the append()
method) or string methods in your solution.

Stage 7
Write a function called remove_value(my_list, remove_position) that takes a list and a remove_position as
parameters. The function returns a copy of the list with the item at the index specified by remove_position,
removed from the list. Check for the remove_position value exceeding the list (my_list) bounds. If the
remove_position is greater than the length of the list, remove the item at the end of the list. If the
remove_position is less than or equal to zero, remove the item stored at the start of the list. You must use a loop in
your solution. You may make use of the list_name.append(item) method in order to build the new list. You must
not use built-in functions (other than the range() function), slice expressions, list methods (other than the append()
method) or string methods in your solution.

Stage 8
Finally, check the sample output in the section titled ‘Sample Output – Part I’ and, if necessary, modify your code so
that:
 The output produced by your program is formatted EXACTLY the same way as the sample output provided.
 Your program behaves as described in these specifications and in the same way as the sample output.

联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!