Code is not working for Cheapest Item function

Post your questions, doubts and issues related to Python Programming
no avatar
Posts: 3
Joined: Wed Apr 25, 2012 9:44 am

Code is not working for Cheapest Item function

by sunilkaushal83 » Wed Apr 25, 2012 9:49 am

# program to administer shoe warehouse dispatching service
# author: <enter your id>
# date last modified:

import string

# This program is written to read the file "orders.txt" that stores separate order details
# on each line in the file, with six items per line as follows :
# ShoeStyle(e.g. Womens/Mens/Children) ShoeType (sandals/runners/dress/school/boat)
# Shoeitemcode(NNNNX) ShoeSize Price ShopAddress

# list of constants used to access each column of order details in the orders.txt file.
DATA_FILE = "orders.txt"
SIZE = 3

def menu():
print "\n\tWarehouse dispatch system Menu\n"
print "1. Display all the orders on file"
print "2. Display a list of the ShoeStyle and ShoeType of all items ordered"
print "3. Display the order details given a particular item code"
print "4. Display the item number of the cheapest item on order"
print "5. Display the order details for the largest shoe size on order"
print "6. Display the total number of items in the order"
print "7. Count and display the Shoe Style and ShoeType of all shoes ordered for a specified Shop Address"
print "8. Display the Shoe Type and price for all ordered items in a given Shoe Style (womens/mens/children)"
print "9. Has a specified Shoe Code been ordered? For which Store Location?"
print "10. Display the cost of a particular item, given the line of data from the orders file"
print "11. Calculate the total cost of the entire order"
print "12. Add details for a new order to the orders file"
print "20. Exit"

option = input("\nEnter the number shown to select a menu item: ");
return option

# Displays (unformatted) all details of all of the orders in the orders.txt input file
def displayFile(inf):
for line in inf:
# display the current line without the end-of-line character, '\n'
print line[:-1]

# displays a list of ShoeStyle and ShoeType of all the items ordered
def displayItemNames (inf):
print "not yet implemented"
#for line in inf:
# not yet implemented

# displays the order details (formatted) of a particular item, given item number
def displayItemDetails(inf, itemcode):
print "Details for ordered item:",itemcode
for line in inf:
#display details for line
splitLine = string.split(line)
if splitLine[ITEM_CODE] == itemcode:
print "Shoe Style: ", splitLine[SHOE_STYLE]
print "Shoe Type: ", splitLine[SHOE_TYPE]
# print "Shoe Size: ",
# print "Price: $ ",
# print "Store Location: ",

# displays, in formatted form, the details of a single
# purchase, given a line of the purchases file
def displayLineDetails(lineString):
splitLine = string.split(lineString)
print "Order details for this item are: "
print "Shoe Style: ", splitLine[SHOE_STYLE]
print "Shoe Type: ", splitLine[SHOE_TYPE]
print "Shoe Size: ", splitLine[SIZE]
print "Item price: $", splitLine[PRICE]
print "Store Location: ",splitLine[SHOPADDRESS]

# Returns the item number of the cheapest item purchased
def findCheapestItem(inf):
# initialise starting value for minprice well above typical shoe prices
minprice = 0
for line in inf:
#itemcode = string.split(line)[ITEM_CODE]
price = eval(string.split(line)[PRICE])
if minprice>price
print minprice
# identify the item number
# identify the item price
# if the item price is below the current minimum then
# change the stored minimum price and the associated item code

return minItem # return result to be displayed in main()

# Returns the itemcode of the largest (size) shoe purchased
def findLargestItem(inf):
maxsize = 0 # this initial value is set to be zero so that the real max is found
for line in inf:
itemcode = string.split(line)[ITEM_CODE] # identify the item code
size = eval(string.split(line)[SIZE]) # identify the item size
# if the item size is below the current minimum then
# change the stored minimum size and the associated item name
if size > maxsize:
maxsize = size
maxitem = itemcode
return maxitem #return itemcode found as maximum

# Find the total number of purchased items in the order
def findNumberOfItems(inf):
total = 0 # temporary only, for testing choice == 6 main()
# not yet implemented
# count all lines in file and return total count
return total

# Count and display the Shoe Style and Shoe Type of all items ordered for a particular store location
def displaySectionItems(inf, givenAddress):
#print "Items ordered from Store ", givenAddress, ": "
count =0
print "not yet implemented"
#for each line in file
#get location from file
#location = string.split(line)[SHOPADDRESS]
# if location matches givenAddress
#if givenAddress = location:
#print "Order details: "
# display line details
# increment count value
return 1 #return count to main

# Display the Shoe Type and price for all ordered items in a given Shoe Style (womens/mens/children)"
def displayItemsforStyle (inf, specifiedStyle):
print "Not yet implemented"

# Check whether a specified item has been ordered and if so, which store location
def itemPurchased(inf, specifiedItem):
# set variable found to False
found = False
# for each line of the file
# identify the item code
# if the item code matches the specified item
#set found to True
if found:
return string.split(line)[SHOPADDRESS]
return ""
# return the value of purchased to main()

# Calculate the total cost of purchasing a given single item
# not yet implemented - add function here

# Calculate total cost of all purchases
# not yet implemented - add function here

# add details of another purchase to the file (closed)
# not yet implemented - add function here

def main():
# Open file and display menu
infile = open(DATA_FILE, 'r')
choice = menu()

# Execute menu choice
while choice != 20:
if choice == 1:
elif choice == 2:
displayItemNames (infile)
elif choice == 3:
itemName = raw_input("Enter the code of an item ordered: ")
displayItemDetails(infile, itemName)
elif choice == 4:
item = findCheapestItem(infile)
print "The cheapest item is :", item
elif choice == 5:
item = findLargestItem(infile)
print "The largest shoe on order is :", item, "Details below: "
elif choice == 6:
total = findNumberOfItems(infile)
print "The total number of items purchased is ", total
elif choice == 7:
location = raw_input("Enter the name of a store location: ")
print "The items purchased from store ", location, "were : "
itemCount = displaySectionItems(infile, location)
if itemCount ==1:
print "There was only", itemCount, "item ordered from this store"
print "There were", itemCount, "different items ordered from this store"
elif choice == 8:
style = raw_input("Enter a Shoe Style:")
print "The listed items in this style are ordered:", style
displayItemsforStyle(infile, style)
elif choice == 9:
item = raw_input("Enter the item Code to search for: ")
location = itemPurchased(infile, item)
print "Location ", location
if location != "" :
print "This item has been ordered for ", location
print "This item has not yet been ordered"
elif choice == 10:
# next line provides a temporary test for function calculateCostOfItem()
# print calculateCostOfItem("womens sandals 2653F 7 89.65 Stockland")
print "Not yet implemented"
elif choice == 11:
print "Total purchases: Not yet implemented"
#print "Total purchases cost: $", calculateTotalCost(infile)
elif choice == 12:
print "Not yet implemented"

print "Wrong number chosen - try again"

# Reset file reading position to start of file and display menu again
choice = menu()

# Close file and end program
print "End of program"

# Execute main function

Q: Examine the code and comments of function findLargestItem() (option 4) in order to understand how it works. The function findCheapestItem() (option 5) can be modelled on this. Assume there is only one cheapest item.

Return to Python Programming