One part of my program requires that the user enters a date and this date is then checked against each product in the dictionary to see if the date the product arrived plus its shelf life causes the product to expire before or after the date entered by the user.
import sys
from string import *
import pickle
import datetime
cheeseDictionary = {}
userInput = ""
def loadProduct(fileName):
global cheeseDictionary
f = open(fileName,"r")
line = f.readline() # Reads line from file
while line:
line = line[:-1]
data = split(line,":") # Splits line when there is a colon
cheeseDictionary[data[0]] = {"date":data[1], "life":data[2], "name":data[3]} # Stores each split item
line = f.readline() # Next line
f.close()
def saveProduct(fileName,cheeseDictionary):
f = open(fileName, "w")
for i in sorted(cheeseDictionary.keys()):
v = cheeseDictionary[i]
f.write("%s:%s:%s:%s\n" % (i, v["date"], v["life"], v["name"]))
f.close()
def printProduct(cheeseDictionary):
print "ID"," ","Date"," ","Life(days)"," ","Name"
for cheese in cheeseDictionary:
print cheese," ",cheeseDictionary[cheese]["date"]," ",cheeseDictionary[cheese]["life"]," ",cheeseDictionary[cheese]["name"]
def addProduct():
global cheeseDicitonary
correct = 0
idInput = ""
dateInput = ""
lifeInput = ""
nameinput = ""
while correct != 1:
idInput = raw_input("Please enter the ID of the cheese to be added. ")
if cheeseDictionary.has_key(idInput):
print ("This ID already exists. Please try again.")
correct = 0
else:
newID = idInput
correct = 1
dateInput = raw_input("Please enter the date of the cheese to be added in the format dd/mm/yyyy. ")
lifeInput = raw_input("Please enter the life of the cheese to be added in days. ")
nameInput = raw_input("Please enter the name of the cheese to be added. ")
cheeseDictionary[idInput] = {"date":dateInput, "life":lifeInput, "name":nameInput}
def checkProduct(cheeseDictionary):
dateCheck = raw_input("Please enter the date in the format dd/mm/yyyy: ")
for cheese in cheeseDictionary:
I know I need to change the dates store din the dictionary into the date time format but I am unsure how to do this. Thanks for any advice given :)