Sunday, August 2, 2015

Intro to Programming with Python - Tutorial 7 - Exercise 1

In tutorial 7 in the intro to programming with python series we covered python dictionaries.
The YouTube tutorial can be found here:

The exercise for this tutorial is to build a dictionary for class grades.
Initialize the grades dictionary with five names and marks (You pick names and marks)

Then write a code block that will ask the user to enter a name. 
If name is found in the grades dictionary tell the user what his/her grade is. Then ask if they wish to change the grade. If answer is 'Yes' prompt them to enter a new grade and replace the old one in the dictionary.
If the name is NOT found in the grades dictionary prompt the user to enter a mark and add the name and associated mark to the grades dictionary.

Repeat this FIVE times (Copy and paste this code block)

When done tell the user what the grades dictionary looks like.

Here is a sample output from running the program.


Here is the program:
# Define the five key value pairs in our initial grades dictionary
# You can separate the key value pairs onto individual lines for clarity
grades = {
'John Smith' : 80,
'Jane Austin' : 82,
'Mohammed Ali' : 86,
'Janet Jackson' : 76,
'Bob Marley' : 92
}
# Now ask the user, five times
name = input('Enter a name: ')
if name in grades:
print('We have %s with a mark of %s' % (name, grades[name]))
replaceMark = input('Do you wish to change the mark (Yes/No)? ')
if replaceMark == 'Yes':
mark = int(input('Enter the new mark: '))
grades[name] = mark
else:
print('We do not have %s in our grades dictionary' % name)
mark = int(input('Enter a new mark for %s: ' % name))
grades[name] = mark
name = input('Enter a name: ')
if name in grades:
print('We have %s with a mark of %s' % (name, grades[name]))
replaceMark = input('Do you wish to change the mark (Yes/No)? ')
if replaceMark == 'Yes':
mark = int(input('Enter the new mark: '))
grades[name] = mark
else:
print('We do not have %s in our grades dictionary' % name)
mark = int(input('Enter a new mark for %s: ' % name))
grades[name] = mark
name = input('Enter a name: ')
if name in grades:
print('We have %s with a mark of %s' % (name, grades[name]))
replaceMark = input('Do you wish to change the mark (Yes/No)? ')
if replaceMark == 'Yes':
mark = int(input('Enter the new mark: '))
grades[name] = mark
else:
print('We do not have %s in our grades dictionary' % name)
mark = int(input('Enter a new mark for %s: ' % name))
grades[name] = mark
name = input('Enter a name: ')
if name in grades:
print('We have %s with a mark of %s' % (name, grades[name]))
replaceMark = input('Do you wish to change the mark (Yes/No)? ')
if replaceMark == 'Yes':
mark = int(input('Enter the new mark: '))
grades[name] = mark
else:
print('We do not have %s in our grades dictionary' % name)
mark = int(input('Enter a new mark for %s: ' % name))
grades[name] = mark
name = input('Enter a name: ')
if name in grades:
print('We have %s with a mark of %s' % (name, grades[name]))
replaceMark = input('Do you wish to change the mark (Yes/No)? ')
if replaceMark == 'Yes':
mark = int(input('Enter the new mark: '))
grades[name] = mark
else:
print('We do not have %s in our grades dictionary' % name)
mark = int(input('Enter a new mark for %s: ' % name))
grades[name] = mark
print('Here is the updated grades dictionary: ', grades)

No comments:

Post a Comment