Created
May 8, 2017 14:54
-
-
Save gauravpatt92/33694ac13652a017118ae2e5d455e426 to your computer and use it in GitHub Desktop.
Adding birthdays and searching for them in a json file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
answer='yes' | |
while answer!='no': | |
with open("birthdays_1.json","r") as f_r: | |
data = json.load(f_r) | |
print("\n\nWe have birthdays of the following people...") | |
for i in data: | |
print ("\n" + i) | |
ans = input("1. Find somebody's birthday. \n2. Add birthday \n\n") | |
if ans=='1': | |
name = input("Enter the person's name: ") | |
print ("The birthday for {} is {}".format(name, data.get(name, "not in our database"))) | |
elif ans=='2': | |
n = int(input("How many birthdays do you wanna add? (max 3 at a time) ")) | |
i=0 | |
while i<n: | |
print("\nScientist ",i+1) | |
new_name = input("\nEnter the name: ") | |
new_birthday = input("Enter their birthday (dd/mm/yyyy):") | |
data[new_name] = new_birthday | |
with open("birthdays_1.json", "w") as f_w: | |
json.dump(data, f_w) | |
print ("Birthday Added!") | |
i+=1 | |
else: | |
print("\nInvalid Choice!") | |
answer = input("\nDo you wanna use again (yes/no): ") | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"Ben Franklin": "17/1/1776", | |
"James Watt": "30/1/1736", | |
"Albert Einstein": "14/3/1879", | |
"Neils Bohr": "7/10/1885", | |
"Clark Maxwell": "13/6/1831", | |
"Richard Fienmann": "11/5/1918"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment