King’s College London¶
Faculty of Arts & Humanities¶
Coversheet for submission of coursework¶
(Undergraduate & Taught Postgraduate)¶
Complete all sections of this cell. Failure to complete this as required may result in your work not being accepted for assessment.
Module Code: 5AAVC210
Module Title: Introduction to Programming
Assignment: Assignment 1
Deadline: 19 Feb 2019 before 4pm
DECLARATION BY STUDENT
This assignment is entirely my own work. Quotations from secondary literature are indicated by the use of inverted commas around ALL such quotations AND by reference in the text or notes to the author concerned. ALL primary and secondary literature used in this piece of work is indicated, and dependence upon ANY source used is indicated at the appropriate point in the text. I confirm that no sources have been used other than those stated.
I understand what is meant by plagiarism and have signed at enrolment the declaration concerning the avoidance of plagiarism. I understand that plagiarism is a serious academic offence that may result in disciplinary action being taken. I understand that I must submit work BEFORE the deadline, and that failure to do so will result in capped marks.
Complete the following:¶
Candidate number:
(This is a letter followed by five digits, and can be found on Student Records)
PLEASE READ¶
You are very welcome to use online resources to help you carry out these tasks. You MUST cite (in the code comments or in an adjacent cell) if you use code or tutorials that you have got from another source. A brief statement and a link to the material is considered acceptable acknowledgement.
Task 1¶
You are going to write a program to tell users about a specific country. A file called countryinfo.py, which is included in the .zip file along with this Notebook, is a list of dictionaries detailing the majority of countries in the world. Make sure you have this countryinfo.py file in the same folder as this Jupyter Notebook.
Dealing with dicts inside a list sounds complex. The first part of this tutorial should help you understand the structure: https://canvas.instructure.com/courses/1133362/pages/book-5-dot-4-python-combining-lists-and-dictionaries
I’ve imported the countryinfo.py file in the cell below, alongside the pprint() function, which works just like print(), but makes complex types easier to read by putting spaces and new lines in them. Run it and see the contents of the file. (You can also open the file in a text editor to view it.)
In [ ]:
# we’re importing a variable here from a local file
from countryinfo import countries
from pprint import pprint # this is the “pretty print” function
pprint(countries)
When you run pprint(countries) you can see all the dict items with their keys and values. (These are all contained within a list called countries, which is what we imported, above.)
Write a program which:
• Asks the user which country they wish to find out about (NB: they’ll have to spell it correctly and start it with a capital letter). [10 marks]
• Looks to see if the country is within a dict item in the list, displaying a message if no such country is found. [10 marks]
• If the country name is found, prints out facts about the country using the information in countryinfo.py. [25 marks] For example, if your user entered ‘Chile’, your program would return something like:
You asked about: Chile
Capital city is:
Santiago
Timezone is:
[‘America/Santiago’, ‘Pacific/Easter’]
Continent is:
South America
Hint¶
In the cell below I’ve given you a way of looping through all the dict items in the countries list and extracting the value of the name variable. Play around with this and see how it works.
In [ ]:
for item in countries:
countryname = item[‘name’]
print(countryname)
Write your Task 1 code below:¶
In [ ]:
# your Task 1 code here. You can use multiple cells if you wish.
Here, please explain your code, i.e. how you approached the task: **[5 marks]**
My code…
Task 2¶
Let’s make a game:
• Ask a user to guess a country based on its capital city. The capital city should be selected randomly from the countryinfo.py file. The user inputs their guess and get told if their guess is correct or incorrect. [15 marks]
• We want the user to guess ten countries in total. If they guess the country correctly, according to the information in the countryinfo.py file, they get 1 point; if they do not, they get 0 points. These points should be stored in variables that act as counters. [10 marks]
• The program stops when they have had 10 tries at guessing the country. [10 marks]
• The user’s score (max 10, min 0) should be presented to them at the end. [10 marks]
Explore the data and decide on the best way to write your code!
Hint: random.choice()
A function called random.choice() takes a list and returns a random value from it. You can see how this function works in the example below. This might come in useful!
In [ ]:
from random import choice
# run this code a couple of times to see what happens
some_list = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’,]
print(choice(some_list))
Write your Task 2 code below:¶
In [ ]:
# your Task 2 code here. You can use multiple cells if you wish.
Here, please explain your code: **[5 marks]**
My code…
Finally…¶
To submit Project 1 you must upload both the Jupyter Notebook (to the Jupyter Notebook folder) and a PDF of your Jupyter Notebook (to the Turnitin folder) by the submission deadline.