讲解CE151、辅导Python程序语言、辅导Python设计、讲解hierarchical structure

- 首页 >> Python编程

Python Programming

CE151 Assignment 2 2018-19

Specification

Outline

Your task is to write a program which can take as input various data files about different cities in

China. The program will then be able to answer simple questions about those cities.

The task involves several key skills which have already been developed in the labs:

1. Interaction with the user,

2. File handling,

3. Use of data structures,

4. Incorporation of hierarchical structure.

There is a template called assignment_2_blank.py which you should use as a starting point.

File Format

There will be a text file with information for each city. This will be in the same format as for Lab 6,

except that it now has the city name on the first line:

Xi'an

China

8705600

Northwest Chang'an Shaanxi_Normal Xidian

34 16 N 108 54 E

3 3 8 15 20 25 26 25 20 15 8 2

There will be several text files (uploaded on Moodle as assignment_2_city_files), each for a different city.

Data Structures

You will need a global variable called cities. This will contain a dictionary. Each key in the

dictionary will be a city e.g. 'xian'. The form of the city name when it is the dictionary key will be all

lower case with no single quotes. i.e. it will be 'xian' and not 'Xi\'an'.

The corresponding value will be another dictionary, just as for Lab 6, with the city name added:

{ 'name': 'Xi\'an', 'country': 'China', 'population': 8705600,\

'universities': [ 'Northwest', 'Chang\'an', 'Shaanxi Normal',\

'Xidian' ],\

'location': ( 34, 16, 'N', 108, 54, 'E' ),\

'temperature': [ 3, 3, 8, 15, 20, 25, 26, 25, 20, 15, 8, 2 ] }

You will thus be able to store information about multiple cities. So:

2

cities[ 'xian' ]

will be the above dictionary containing information about Xi'an.

choose_task()

As in Assignment 1, this will be the top-level function, but it will work in a different way. When

choose_task() is run it will prompt the user for a single number in the range 0 to 2. Depending on the

value entered it will:

0. Exit

1. Call load_city_data()

2. Call answer_questions()

When one function has been called and has finished executing, choose_task() will again prompt the

user for a number. This will continue until '0' is entered by the user.

Hints on choose_task():

choose_task() can be implemented as a While loop. You are permitted to do while True

and then break when the input is 0.

To read the numerical input each time round the loop you can use input() and convert to int using

int() before testing its value.

You can assume that the user always gives correct inputs, i.e. always an integer 0-2.

Please print a suitable prompt on the screen so that it is clear that the user must input something.

Notice that your program will have a nice multiple-level structure: At the top we have choose_task().

Depending on the input, it calls one of the other functions. Each of those functions may also have

multiple levels. When one of the functions finishes, control returns to choose_task() for the next choice

of function.

load_city_data()

When called, this function will prompt the user for the name of a city, e.g. Beijing. It will then:

1. Convert the name into lower case

2. Remove any single quotes '\'' e.g. in 'Xi\'an'

3. Add '_info.txt' to the end

4. Open the file and read the contents

5. Add the data to the global variable cities, as described above.

So for example, suppose the name of the city entered by the user is 'Xi\'an'. We convert to lower case

and remove the '\''. We then add '_info.text' to the end. The filename is thus 'xian_info.txt'. We extract

the data from this file and add it to the global variable cities which is a dictionary.

So, we will now have

cities[ 'xian' ] = { 'name': 'Xi\'an', 'country': 'China',......... }

If the resulting filename does not exist, your function should give an error message:

'No such city'

It should not just crash. There will be marks for this - see below.

If the file containing information about the city exists but is not in the right format, you should try not

to crash but to give an error message instead. There will be extra marks for this - see below.

3

answer_questions()

This function will prompt the user for a question and it will then answer it and prompt for another

question. This will continue until the user enters a blank line, i.e. they press Enter with no question.

answer_questions() will then finish executing and control will return to choose_task() above.

These are the questions you should be able to answer:

1) population <city>

e.g.

population Xi'an

population Beijing

population Souzhou

This will simply return the population of the city in question.

Note that you will need to bring the city down to lower case and to remove '\'' to make it match the

dictionary cities. Youl should create a function to do this and use the same function above as well.

If the requested city does not exist, you should return an error 'No such city'. The program

should not crash.

2) population <city-list>

e.g.

population Xi'an Beijing Souzhou

This will return the total population of all the cities in the list, added up. The whole command is on

one line, separated by spaces. If one of the countries in the list does not exist, the program should not

crash; it should give an error message instead.

3) largest population

This will return the name of the city with the largest population, together with its population in a

message like this:

The largest city is Beijing. It has a population of 21700000.

4) temp Beijing October

If Beijing is city with data currently in the program, and if Oct is a valid month, it should say:

The average temperature in Beijing during October is 13 Centigrade.

5) position Beijing

If Beijing data is not currently read in, this should give an error message. Otherwise, it should print

out

The position of Beijing is 39°55′N 116°23′E

4

(The 'degree' symbol after the 39 is '\u00B0' in Python).

Your program must conform exactly to the format style of assignment_2_blank.py.

Assessment

We will look at your work in the lab and also test the program which you submit.

Submission Deadline and Procedure

Set on Wednesday 19th December, NWU Week 16

Due on 21h59 Wednesday 2nd January 2019, NWU Week 18

Submit one .py file containing your program to Essex Moodle (Assignment 2 - Submission Link):

moodle.essex.ac.uk.

If your NWU ID number is 12345678 then the file you submit should be called a12345678.py .

Note that we have added 'a' to the start of the filename because a filename starting with a digit cannot

be imported into Python.

In addition to submitting, you might need to demonstrate your work in the lab.

Marking Scheme

Demonstration of the following in the lab, and verification of submission:

10%: load_city_data() works with valid country name and valid data in file

10%: load_city_data() gives error for invalid country name and does not crash

10%: load_city_data() gives error for invalid data in file and does not crash

10%: answer_questions(): query population <city-list> works for valid cities in list

10%: answer_questions(): query population <city-list> gives error for invalid cities and

does not crash

10%: answer_questions(): query largest population works

5%: answer_questions(): query of type temp Beijing October works

5%: answer_questions(): query of type position Beijing works

30%: correct format of program (see below)

----

100%

This is how we will check the format:

5% Filename of the program is correct.

5% Comment at the start has the correct layout, has the correct filename and explains what

program does.

5% Comment 'To run this program' has been edited to show exactly how to run the program.

5% Each function starts with a comment, exactly as shown in blank2.py etc.

5% Four spaces are used for indentation throughout, no tabs are used at all.

5% Space around arguments exactly follows the examples. e.g. print( x, y ) and not

print (x,y), not print(x,y), not print( x , y ) etc.

Plagiarism

You should work individually on this project. Anything you submit is assumed to be entirely your

own work. The usual Essex policy on plagiarism applies.



站长地图