Python Code
Python Code
import string
import os
"""
def printsomething():
print("Hello from python!")
def PrintMe(v):
print("You sent me: " + v)
return 100;
def SquareValue(v):
return v * v
"""
# function to open and read the grocer items input file & create a dictionary with
the item and frequency
def ItemList():
fileLocation = r'U:\CornerGrocer\CornerGrocer\Release\InputFile.txt'
with open(fileLocation, 'r') as groceryList:
List = groceryList.readlines() # to read the lines in the file and add
them to the list
List.sort() # to sort the list in alphabetical order
# loop that removes the newline from the items in the list
for produceItems in List:
produceList.append(produceItems.strip('\n'))
# function that creates menu option 1 to print the produce item and the frequency
def GroceryListFrequency():
print("List of items and quantities purchased")
ItemList()
Title = '{item:^16}{number:>14}'
print(Title.format(item = 'Produce Item', number = "Total Purchased"))
print()
Line = '{item:>14}{number:.>14}'
for produceItems, count in groceryDict.items(): # loop that prints each item
and counts the frequency
print(Line.format(item = produceItems, number = count))
# function that returns menu option 2 to print one item with the number purchased
for the day
def GroceryItemFrequency(item):
ItemList()
groceryItem = item.capitalize() # makes the first letter capital to read from
file
if groceryItem in groceryDict.keys(): # if statement to get number purchased
of a particular item
num = groceryDict.get(groceryItem) # actually gets the number of that item
and assigns it to num
return num
else:
return 0
# function that creates menu option 3 histogram graph and frequency.dat file
def GroceryHistogram():
ItemList()
with open('frequency.dat', 'w') as graph: # to open the file
for key in groceryDict.keys():
graph.write('{} {}'.format(key, groceryDict.get(key))) # to write the
dict to the file