Skip to content

Instantly share code, notes, and snippets.

@y1n0
Created February 28, 2017 23:49
Show Gist options
  • Select an option

  • Save y1n0/fa38ba8786225bf092e50ed04be1e10c to your computer and use it in GitHub Desktop.

Select an option

Save y1n0/fa38ba8786225bf092e50ed04be1e10c to your computer and use it in GitHub Desktop.
#!/usr/bin/python3.5
from collections import Counter
import json
def help():
print('Available options:')
print('\t\'add <name>\' to add a new entry')
print('\t\'show <name|all>\' to show the birthday.')
print('\t\'count\' to count entries that have the same month.')
print('\t\'help\' to show this menu.')
print('\t\'exit\' to end the program.')
def show_all():
names = list(birthdays.keys())
for i, x in enumerate(names):
print('{}. {}\'s birthday is on {}'.format(i, x, birthdays[x]))
print('Welcome to the birthday dictionary.')
help()
while True:
request = input('> ')
if 'add' in request:
try:
with open('birthdays.json', 'r') as f:
birthdays = json.load(f)
name = request.split()[1]
print('enter {}\'s birthday: '.format(name), '(make sure it\'s in mm/dd/yyyy format)')
birth = input('birthday: ')
birthdays[name] = birth
with open('birthdays.json', 'w') as f:
json.dump(birthdays, f)
print('added!')
except IndexError:
print('\'add\' requires an argument')
elif 'show' in request:
with open('birthdays.json', 'r') as f:
birthdays = json.load(f)
try:
name = request.split('show ')[1]
except IndexError:
show_all()
else:
if name in birthdays:
print('{}\'s birthday is on {}'.format(name, birthdays[name]))
elif name == 'all':
show_all()
else:
print('{} is not on our lists.'.format(name))
elif 'count' in request:
with open('birthdays.json', 'r') as f:
birthdays = json.load(f)
months = []
mon_names = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
for x in list(birthdays.values()):
mon_num = int(x.split('/')[0])-1
months.append(mon_names[mon_num])
print(dict(Counter(months)))
elif 'help' in request or request == '':
help()
elif 'exit' in request:
break
else:
print('this option is not available.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment