Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b6ea61f

Browse files
author
SamLaFell
committed
Create chapter5.py
Create new file to show Dictionaries chapter in Automate the Boring Stuff
1 parent 3120014 commit b6ea61f

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed

‎automate_the_boring_stuff/chapter_5.py

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
birthdays = {'Alice': 'Apr 1',
2+
'Bob': 'Dec 12',
3+
'Carol': 'Mar 4'
4+
}
5+
6+
while True:
7+
print('Enter a name: (blank to quit)')
8+
name = input()
9+
if name == '':
10+
break
11+
12+
if name in birthdays:
13+
print(birthdays[name] + ' is the birthday of ' + name)
14+
else:
15+
print('I do not have birthday information for ' + name)
16+
print('What is their birthday? (Format: ex, Jun 18)')
17+
bday = input()
18+
birthdays[name] = bday
19+
print('Birthday database updated.')
20+
break
21+
22+
23+
# for v in birthdays.values():
24+
# print(v)
25+
#
26+
# for k in birthdays.keys():
27+
# print(k)
28+
#
29+
# for i in birthdays.items():
30+
# print(list(i))
31+
#
32+
# for k, v in birthdays.items():
33+
# print('Key:', k, 'Value:', v)
34+
35+
import pprint
36+
#
37+
# message = 'It was a bright cold day in April, and the clocks were striking thirteen.'
38+
# count = {}
39+
# for character in message:
40+
# count.setdefault(character, 0)
41+
# count[character] += 1
42+
# print(count)
43+
# pprint.pprint(count)
44+
#
45+
# # Can store pprint in a variable
46+
# pretty_count = pprint.pformat(count)
47+
# print(pretty_count)
48+
49+
# theBoard = {'top-L': ' ', 'top-M': ' ', 'top-R': ' ', 'mid-L': ' ', 'mid-M': ' '
50+
# , 'mid-R': ' ', 'low-L': ' ', 'low-M': ' ', 'low-R': ' '}
51+
#
52+
# def printBoard(board):
53+
# print(board['top-L'] + '|' + board['top-M'] + '|' + board['top-R'])
54+
# print('-+-+-')
55+
# print(board['mid-L'] + '|' + board['mid-M'] + '|' + board['mid-R'])
56+
# print('-+-+-')
57+
# print(board['low-L'] + '|' + board['low-M'] + '|' + board['low-R'])
58+
#
59+
# turn = 'X'
60+
# for i in range(9):
61+
# printBoard(theBoard)
62+
# print('Turn for ' + turn + '. Move on which space?')
63+
# move = input()
64+
# theBoard[move] = turn
65+
# if turn == 'X':
66+
# turn = 'O'
67+
# else:
68+
# turn = 'X'
69+
#
70+
# printBoard(theBoard)
71+
72+
73+
# Nested Dictionaries and Lists
74+
75+
allGuests = {'Alice': {'apples': 5, 'pretzels': 12},
76+
'Bob': {'ham sandwiches': 3, 'apples': 2},
77+
'Carol': {'cups': 3, 'apple pies': 1}}
78+
79+
# def totalBrought(guests, items):
80+
# numBrought = 0
81+
# for k, v in guests.items():
82+
# numBrought = numBrought + v.get(items, 0)
83+
# return numBrought
84+
#
85+
# print('Number of things being brought:')
86+
# print(' - Apples ' + str(totalBrought(allGuests, 'apples')))
87+
# print(' - Cups ' + str(totalBrought(allGuests, 'cups')))
88+
# print(' - Cakes ' + str(totalBrought(allGuests, 'cakes')))
89+
# print(' - Ham Sandwiches ' + str(totalBrought(allGuests, 'ham sandwiches')))
90+
# print(' - Apple Pies ' + str(totalBrought(allGuests, 'apple pies')))
91+
92+
93+
# Chapter 5 Project:
94+
95+
def displayInventory(inventory):
96+
print('Inventory: ')
97+
itemTotal = 0
98+
for k, v in inventory.items():
99+
k = k.capitalize()
100+
if v > 1:
101+
k = k + 's'
102+
itemTotal += v
103+
print(v, k)
104+
print('Total number of items:', itemTotal)
105+
106+
107+
inv = {'rope': 1, 'torch': 6, 'gold coin': 42, 'dagger': 1, 'arrow': 12}
108+
109+
displayInventory(inv)
110+
111+
112+
def add_to_inventory(inventory, addedItems):
113+
for item in addedItems:
114+
if item not in inventory.keys():
115+
inventory[item] = 1
116+
else:
117+
inventory[item] += 1
118+
return inventory
119+
120+
121+
inv = {'gold coin': 42, 'rope': 1}
122+
dragonLoot = ['gold coin', 'dagger', 'gold coin', 'gold coin', 'ruby']
123+
inv = add_to_inventory(inv, dragonLoot)
124+
displayInventory(inv)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /