Python辅导 | Programming Assignment G – 4478/8936
Page 1 of 12
Programming Assignment
Introduction to Information Technology / G – 4478/8936
Note 1: Only use IDLE (with Python 3.6 or above) to write this assignment! For the purpose of this
assignment, you are not allowed to use any other Python IDE and you are not allowed to use any other
version of the Python.
Note 2: Familiarise yourself with the Python Style Guide at the end of this document.
Note 3: All cases of plagiarism will be pursued! If in doubt, consult the Student Academic Integrity Policy
http://www.canberra.edu.au/current-students/canberra-students/conduct
This assignment will test a student’s knowledge of, and skills in writing application software for a task,
understanding the business rules of a particular problem, coding these in a computer program, developing
a graphical user interface, reading data from a text file on disk, and connecting to an SQL database from
within the program. As such, the assignment requires you to integrate and synthesise what you have learnt
so far in this unit, in order to design and create a correct working solution.
The context of this programming assignment is human Space Exploration (possibly inspired by the 50th
anniversary of the moon landing!).
For this assignment, students will use the Python programming language (version 3.x) and development
will be on the IDLE platform as practised in the computer lab classes. This assignment consists of four
stages.
• Stage 1: A simple Python program using an interactive text based menu (no GUI)
• Stage 2: The same (stage 1) but wrapped in a GUI
• Stage 3: Input comes from a text file – read only once, then information stored in a suitable data
structure. (Advanced GUI)
• Stage 4: Input comes from an SQL database file. Generates a Database Report.
4478 IIT 8936 IIT G
Part A – Stage 1 (25 marks) Part A – Stages 1 and 2 (27 marks)
Part B – Stage 2 (10 marks) Part B – Stage 3 (13 marks)
Part C – Stage 3 (15 marks) Part C – Stage 4 (10 marks)
Bonus* – Stage 4 (up to 10 marks)
*Stage 4 is a bonus stage for undergraduate students to give you a stretch target. It allows you to pick up extra marks to make up
for marks lost elsewhere in the assignment but note that you cannot achieve more than 50/50 marks.
Planetary Exploration App
Our astronauts are about to leave for a trip to outer space. They may be visiting the Moon, Mercury, Venus,
Mars or one of the larger moons of Jupiter (IO, Europa, Ganymede or Callisto). Each astronaut has a mass
allowance in kg for items that they can take along on the trip, including tools and personal items. The
amount they’re allowed depends on their job description.
Flight crew are allowed a total of 100kg; Mission specialists are allowed a total of 150kg.
Page 2 of 12
Each of these solar system bodies (celestial bodies) has less gravity than the earth. For example, this means
that a mass of 100kg on earth will weigh 100kg, but on the Moon, a mass of 100kg will weigh the equivalent
of 16.6kg.
You will be calculating how much mass each astronaut has left over for personal items, the total available
mass and the average available personal mass allowance across all six astronauts. You will also calculate
the weight of the average available personal mass allowance on the celestial body the astronauts are
travelling to.
Stage 1:
You will need a list that contains the names of the celestial bodies that our astronauts may travel to and
their Mass multipliers:
Celestial body Mass Multiplier
Mercury 0.378
Venus 0.907
Moon 0.166
Mars 0.377
Io 0.1835
Europa 0.1335
Ganymede 0.1448
Callisto 0.1264
These details can be hardcoded in your program.
In Stage 1, you will be developing a Python program without a GUI. Input and output are via the Python
shell. Write a Python program that displays a text based interactive menu to allow the users to:
• Display list of celestial bodies that are possible destinations along with their mass multipliers
• Display the weight allowances for the two different types of astronaut
• Calculate personal mass allowances along with the total mass available
• Calculate the average available mass available for personal items
Students may use a nested list to store the name of the celestial body and its mass multiplier. You can
assume there are three crew astronauts and three mission specialists. You may wish to store the mass of
their tools in a list as well.
Notes:
• You should use functions for each task in the program, for example a printMenu function to print
a menu and calculcateAverageMass to calculate the average available mass etc.
• Your program should have a main() function to appropriately direct the program.
What the tutors will be looking for
The tutor’s instructions include:
• Constants vs literals. Using constants is important for the ease of maintenance. Not using
constants will result in lower marks. For example, consider constants for the mass multipliers for
each celestial body and the upper limit for each type of astronaut.
Page 3 of 12
• Program code layout. Separate blocks of code by a blank line. Use comments.
• Program is written using well-defined functions including a main() function to direct the program.
• A comment is not an essay. Comments are important for the maintenance of a program and should
contain enough details but keep them concise. Do not comment every single line.
• The program must have a prologue. Check style against the Python style guide attached below.
• Good names for your variables and constants. Check style against the Python style guide attached
below.
• Does the program work correctly?
Please refer to the Programming Assignment Marking Rubric for details on marks.
Step-by-Step Guide for Stage 1
1. Think about your strategy for how you will display the options for user to choose from, how you
will calculate the available personal item mass for each astronaut and how to calculate the average
available mass and average available weight. Think about writing simple functions for the
repetitive calculations.
2. Create a new Python file, you may call it ProgAsgStage1
3. In the Stage1 class (file ProgAsgStage1.py), you may put all code for the user interaction and
the calculation into the main method. (You might still need to define global variables and constants
outside the main method at the top of the editor window). You might like to use nested lists to
hold the name and mass multiplier of the celestial bodies.
4. You will need to write a function to print a menu to the user. One possible example is:
Page 4 of 12
5. Now add the code that implements your strategy to display the different destinations with their
mass multipliers TEST
6. Add the code to display the weight allowances for the two different types of astronaut TEST
7. Add the code to calculate the personal mass allowances of each of the six astronauts along with
the total available mass TEST
8. Add the code to calculate the average available mass allowance and weight on destination TEST
9. Finally, add print statements to print the output to the user.
10. Test your implementation with the test cases mentioned below (and additionally your own).
For steps 7 & 8 work out your tests before you do any coding
Test cases:
Test 1
Destination Selected The Moon
Entered tool weights for crew 100, 100, 100
Entered tool weights for mission specialists 150, 150, 150
Available mass for astronauts: 0,0,0,0,0,0
Total Available Mass 0 kg
Average available mass: 0.0 kg
Average available weight on destination 0.0 kg
Test 2
Destination Selected Mars
Entered tool weights for crew 90, 90, 90
Entered tool weights for mission specialists 140, 140, 140
Available mass for astronauts: 10,10,10,10,10,10
Total Available Mass 60 kg
Average available weight 10 kg
Average available weight on destination 3.77 kg
Test 3
Destination Selected Venus
Entered tool weights for crew 99, 98, 101
Entered tool weights for mission specialists 150, 149, 151
Available mass for astronauts: 1,2,-1,0,1,-1
Total Available Mass 2 kg
Average available mass: 0.333333 kg
Average available weight on destination 0.302333 kg
Test 4
Destination Selected Mercury
Entered tool weights for crew 101, 101, 101
Entered tool weights for mission specialists 151, 151, 151
Page 5 of 12
Available mass for astronauts: -1,-1,-1,-1,-1,-1
Total Available Mass 2 kg
Average available mass: -1.0 kg
Average available weight on destination -0.378 kg
Page 6 of 12
Stage 2:
In stage1, the user input and output is not very satisfactory from the human computer interaction and
usability point of view, your task in this stage is to design and implement a Graphical User interface (GUI)
using buttons and labels for the Astronaut Allowance Calculator that provides an easy to use interface.
This GUI application must allow a user to input the various data elements. To this end, the user can
• input the data using an entry widget
• click on a Calculate button that starts the calculation of the available personal weight allowances,
total and average values and
• an Exit or Quit button to properly close the program.
Use the same code for the calculations from Stage 1. Reusing code is an important part of software
development.
You have a great degree of freedom in what GUI elements you choose and how you would like to design
the layout of your GUI. What matters is the functionality of the design and that the user can input the
required data in a sensible fashion.
What the tutors will be looking for
The tutor’s instructions include:
• Constants vs literals. Using constants is important for the ease of maintenance. Not using
constants will result in lower marks. For example, in addition to Stage 1, consider constants for the
default width and height of the labels, buttons and entry widgets to easily achieve a uniform look.
• GUI Design. Is it simple to use and easy to understand? Are all required components there? Grid
layout is used to place widgets.
• Program is written using well-defined functions including a main() function to direct the program.
• Program code layout. Separate blocks of code by a blank line. Use comments.
• Separate GUI functionality from calculations. Students should use a separate method for the
calculations. It is good practice and follows good programming style to separate GUI code from
other code.
• A comment is not an essay. Comments are important for the maintenance of a program and should
contain enough details but keep them concise. Don’t comment every single line.
• The program must have a prologue. Check style against the Python style guide attached below.
• Good names for your variables and constants. Check style against the Python style guide attached
below.
• Does the program work correctly?
Please refer to the Programming Assignment Marking Rubric for details on marks.
Stage 3:
Complete the Stage 1 & Stage 2 plus: The program will determine the list of chook food at run time. Do
this by reading this data from the file astronaut.txt and data will be stored in a nested list. austronaut.txt
is available on IIT Canvas site.
The file astronaut.txt contains the astronaut tool allowance weights along with their destination. Each line
consists of destination as the first element, followed by a comma, that destination’s mass multiplier
followed by comma and the weight of each astronaut’s tools in kgs.
Page 7 of 12
Moon,0.166,92,93,90,135,140,143
You will need to copy astronaut.txt into the same directory as your Python files. When marking, the
details in the file may change, but the structure will not.
Your task will be to add code to your program that reads the astronaut tool weight data from the text file,
put these into appropriate storage in memory (I suggest you use a nested list) and display the details to
user when requested. Please note that text file contains more destinations than given in stage 1. The list
of different destinations should be displayed to the user by using a listbox widget. You should also use the
advanced Python GUI components to create a graph to compare the mass multipliers of the different
destinations.
Notes:
• Celestial body (destination) should be displayed to the user in ascending order of mass
multiplier.
• The text file should only be read once. You probably want to do that at the start of the program.
Consider how you would do that.
• Write a Function called readFile to read the data from the text file.
What the tutors will be looking for
The tutor’s instructions include (apart from the usual such as good variable names, prologue /
comments, code layout, …)
• Use of constants. Think about where you could use constants here.
• The text file should only be read once. You probably want to do that at the start of the program.
Consider how you would do that.
• Program is written using well-defined functions including a main() function to direct the program.
• Correct display of mass multipliers based on the details in the text file.
• There are various ways you can store the data when reading the file. You could use a nested list.
Stage 4: bonus stage for 4478 and compulsory for 8936 students.
Here, the input will now come from an SQL database contained in the MS Access file astronaut.mdb.
This file is available on the IIT site on Canvas. There is only one table in the database tblAstronaut
tblAstronaut contains fields Desination, MassMultiplier, CrewToolWeight1, CrewToolWeight2,
CrewToolWeight3, MSToolWeight1, MSToolWeight2, MSToolWeight3. A typical record would be
<Titan, 0.143, 80, 71, 93, 132, 145,150>.
Write a Python program using a GUI (Note: extending your GUI program from stage 3 is acceptable, but
you could also write a separate Python file for Stage 4, either way is ok)
• Let the user select the name of a destination (e.g. from a list) and display the mass multiplier along
with the astronauts’ tool weights.
• Create a database report consisting of all destinations and the average available weight on each
destination. You report should include Destination, Mass Multiplier, Total Available tool mass,
average available tool mass and average available weight on destination. The report should be in
ascending alphabetical order of Destination. It should be written to a file “database_report.txt”.
Page 8 of 12
Make sure that your columns are properly aligned. Text columns should be left aligned, number
columns right aligned and rounded to 2 decimal places.
What the tutors will be looking for
The tutor’s instructions include (apart from the usual such as good variable names, prologue /
comments, code layout …)
• Correct connection to the database.
• Correct identification of all the different destinations and their related data.
• Make sure that the lines in the database reports are split into pages and the columns are lined up
and correctly formatted.
• Program is written using well-defined functions including a main() function to direct the program.
Page 9 of 12
Python Style Guide
General
Your programs should be
• Simple
• Easy to read and understand
• Well structured
• Easy to maintain
Simple programs are just that. Avoid convoluted logic, nested if-statements and loops, duplicating
execution (such as reading files multiple times), repeated code, being “clever”.
Programs can be made easier to read by following the “Layout” and “Comments” guidelines below.
Well-structured code uses functions to help tame complexity.
If programs are simple, easy to understand and well-structured they will be easy to maintain. Easily
maintained programs will also use constants rather than literals and use built-in functions and types.
Layout
The IDLE text editor does a good job of automatically laying out your program and it also helps with the
indentation of the code inside an/a if-statements, loops and functions. It is very important in Python that
you indent your code correctly.
White space
Use white space freely:
• A blank line after declarations
• 2 blank lines before each function declaration
Names
Well-chosen names are one of the most important ways of making programs readable. The name chosen
should describe the purpose of the identifier. It should be neither too short nor too long. As a guide, you
should rarely need to concatenate more than 3 words.
Letter case
Constants use ALL_CAPITALS separated by underscore if needed
e.g. PI, MAX_STARS
Variables use firstWordLowerCaseWithInternalWordsCapitalised
e.g. cost, numStars
It is a good idea to use the so called Hungarian notation where the first letter (or two) indicate what type
the variable has:
• i for int, e.g. iNum
• f for float, e.g. fFloatingPointNum
• s for String, e.g. sName
• bo for Boolean, e.g. boCar
Functions use firstWordLowerCaseWithInternalWordsCapitalised
e.g. cost(), numStars()
Page 10 of 12
Types of names
1. Names of GUI widgets should have a prefix indicating the type of control (Python TK Interface). The
prefix should be lower case with the remainder of the name starting with upper case.
Control prefix example
Entry Widget
Label Widget
List box Widget
Scrollbar Widget
Button Widget
Radiobutton Widget
Checkbutton Widget
ent
lbl
lst
scroll
btn
rb
chkb
entQuantity
lblResult
lstStudents
yscroll for vertical scroll bar
btnQuit
rbColour
chkbSugar
2. Names of functions that does not return a value should be nouns or noun-phrases
Example:
newSum = sum(alpha, beta, gamma)
or
newSum = sumOfStudents(alpha, beta, gamma)
rather than
newSum = computeSum(alpha, beta, gamma)
3. Names of functions that returns a value should be imperative verbs.
Example:
calculateCost(quantity, price)
rather than
results(newSum, alpha, beta, gamma)
4. Names of Boolean functions should be adjectives or adjectival phrases
Example:
if (empty(list))
or
if (isEmpty(list))
rather than
if (checkIfEmpty(list))
Comments
Comments should explain as clearly as possible what is happening. They should summarise what the code
does rather than translate line by line. Do not over-comment.
Page 11 of 12
In Python, use ##Comment for a one-line comment.
Comments are used in three different ways in a program:
• Heading comments
• Block comments
• End-of-line comments.
Heading comments: These are generally used as a prologue at the beginning of each program and function.
They act as an introduction, also describe any assumptions and limitations. They should show the names
of any files, give a short description of the purpose of the program, and must include information about
the author and dates written/modified.
Block comments: In general, you should not need to comment blocks of code. When necessary, these are
used to describe a small section of following code. They should be indented with the program structure to
which they refer. In this way the program structure will not be lost.
End-of-line comments: These need to be quite short. They are generally used with parameters to functions
(to explain the purpose and mode of the parameter), and/or with variable declarations (to explain the
purpose of the variable), and are not meant to be used for code.
Comment data, not code: Comments about the data – what it is and how it is structured – are much more
valuable than comments about the code.
Prologue
Each of your programs should have a prologue. This is a set of “header comments” at the top of editor
window. It should include
who wrote it ## Author: Damith Herath
when it was written ## Date created: 21 Feb 2019
when it was last changed ## Date last changed: 21 Mar 2019
what it does ## This program does…
what files it reads / writes ## Input: sample.txt, Output: none
Named Constants
Program sometimes employs a special constant used several times in program. By convention Python
programmers create a global variable and name is written in uppercase letters with words separated by
underscore. For instance, SPEEDING_FINE, INTEREST_RATE
The special naming convention reminds the programmer that no reassignments to the variable
should be made during the execution of the program. Since Python allows reassignments to any
variable, hence the programmer is responsible for not changing the value of the variable.
This makes your program easier to maintain. If the speeding fine or interest rate changes, the change only
has to be made in the one place.
Functions
Using functions is one of the simplest and most effective ways of dealing with complexity in a program.
Page 12 of 12
When you write your own function to perform a calculation, it
should not refer to any GUI controls.
Do not mix IO and calculations in a function.
Function that does not return a value
• encapsulates a task
• name should be a npun. Example: sum()
• should only do 1 task.
Function that returns a value
o encapsulates a query
o return type is int, boolean, double…;
o if return type is boolean, name should be an adjective, otherwise name should be a verb.
▪ Examples: isEmpty(list), SquareRoot(number)
o should answer only 1 query.
Comments for each function should include (to be placed on the line before the function starts)
name above rules apply
purpose what it evaluates or what it does
assumptions any assumptions made, particularly on the arguments
Duplicated Code
View any duplicated code with suspicion. Look for a way of factoring the duplicated code into a function.
Built-in functions
Unless you have a good reason, use built-in functions rather than writing (or not writing) your own. They
will be correct, flexible and familiar to a reader.
Types
Using types is an important way of making your intentions clear. Python does not allow you to be sloppy
with your types. In IIT / IIT G, you are expected to choose appropriate types. You are also expected to use
type conversions such as int (23.26)
Simplicity
Simplicity has long been recognised as one of the most important characteristics of programming. There
are many ways of simplifying programs. These include avoiding nested IF statements, nested loops and
complex conditions.