Week 7 Practice Questions-1
Week 7 Practice Questions-1
7. scores['Mary']
8. scores['Fred'][1]
These statements demonstrate how to refer to an item in a dictionary via its key.
Since the value is a list, you can then refer to an item in the list via its index.
9. scoreKeys = scores.keys()
This statement creates a live list of the keys in the “scores” dictionary. Check what it
contains now by typing “scoreKeys”, and then check it again after statement 10.
CSP1150 Page 1
Task 2 – Online account details
You are supposed to create a program which generates account details of an e-library.
Write a program that gets strings containing a person’s first and last name as separate values, and
then displays their “initials”, “name in address book”, and “username”. For example, if the user enters
a first name of “John” and a last name of “Smith”, the program should display
“Initials : J.S.”
“Name in address book: John SMITH”
“Username: jsmith”
Write a program which checks whether the password entered is valid or not. The program should take
a string (password) as an input and give the output as “Your password is valid” or “Your password is
invalid!”
Write a pseudocode before you start coding using python.
Suggested approach: You can create separate variables to check each condition and assign Boolean
values for them, and check if all these variables are True after you examine all the characters in the
password.
Checking for a special character is best achieved by creating a string containing all of the special
characters (add a backslash before the quote mark and the backslash characters in the string so that
they don’t get misinterpreted) and checking if the character is “in” it:
specialChars = '\'~!#$%^*()_+-={}|[]\\:<>?,./' Python
CSP1150 Page 2
Task 4 – Student List for a Subject
A teacher wants to enter the details of the students for his class into an online database and get three
lists of students who follow Mathematics, Science, and Computing.
Write a program that asks the user to enter the student number, name (first name last name), and the
subjects selected out of Mathematics, Science, and Computing. A student can be enrolled in one or
more of the three subjects, and all students are enrolled in one of the three subjects mentioned. The
program should ask the teacher for the number of students in the class and prompt for the student
number, name, and subjects (given in sample input) followed by the students accordingly. When all
the details of the students are entered, the program should display the list of students who are
following each subject in the following format (given in sample output).
Use dictionaries in python to achieve the given task.
Sample Input
Student Number: 4005632
Name: John Smith
Subjects followed: Mathematics, Computing
Sample Output
Mathematics (5)
1. John Smith
2. Jane Doe
3. Harry Potter
4. Peter Parker
5. Jack Reacher
Science (2)
1. Chin Li
2. Jane Doe
Computing (6)
1. Chin Li
2. Jane Doe
3. Harry Potter
4. Peter Parker
5. Jack Reacher
6. John Smith
CSP1150 Page 3
Task 5 – Email Generator
Create a new file in the Python Editor and copy-paste the following code into it:
students = {400123: 'John Smith', 400159: 'Du Pen-Gu', 400527: 'Chin Li', Python
400321: 'Jane Doe', 400357: 'Harry Potter', 400624: 'Peter Parker'}
This simply creates a dictionary named “students” which contains 6 items. The keys are student
numbers, and the values are their names. Your task is to write a program that will generate email
addresses from the student names / numbers provided, implementing the following rules:
a. All characters in an email address should be in lowercase.
b. Dashes (“-”) or any other symbols in a student’s name should not appear their email
address.
c. An email address consist of the first letter of the student’s first name, followed by the
first 4 letters of their surname, joined with “@example.com” (e.g. “John Smith” would
become “jsmit@example.com”). We will assume that a student’s name only consists
of a first name and surname.
d. Email addresses must have a minimum length of 5 characters before the domain name
(before the “@” symbol). If a student’s surname is less than 4 characters long, include
add “0”s (zeroes) to the end (e.g. “Chin Li” becomes “cli00@example.com”).
Write a pseudocode before you start coding using python and make sure there is clear logic to solve
your problem.
Before coding using python, write a pseudocode to get a picture of what exactly needs to be done.
CSP1150 Page 4