Reputation: 23
I'm working on the following problem:
A supermarket wants to reward its best customer of each day, showing the customer’s name on a screen in the supermarket. For that purpose, the customer’s purchase amount is stored in one list and the customer’s name is stored in another list. Implement a function nameOfBestCustomer(sales, customers) that returns the name of the customer with the largest sale. Write a program that prompts the cashier to enter all prices and names, adds them to two lists, calls the method that you implemented, and displays the result. Use a price of 0 as a sentinel.
I have the following code working properly except that it doesn't account for multiple customers with the same purchase amount being the max. Any suggestions on how I could easily fix this problem? I'm obviously new to Python so any other suggestions you might have are also welcome! Thanks!
sales = []
customers = []
def customerSales() :
salesEntry = 0.01
customersEntry = 0
while salesEntry > 0 :
salesEntry = float(input("Enter new purchase amount or a 0 to finish: "))
if salesEntry > 0 :
sales.append(salesEntry)
customersEntry = input("Enter customer name: ")
customers.append (customersEntry)
customerSales()
def nameOfBestCustomer(sales, customers) :
#@param: sales and customers lists
#@return: none
bestCustomer = ""
salesMax = 0
salesMax = max(sales)
index = sales.index(salesMax)
bestCustomer = customers[index]
print("The best customer of the day was " + bestCustomer + ".")
print("They spent $%.2f" % salesMax + ".")
nameOfBestCustomer(sales, customers)
Upvotes: 0
Views: 4587
Reputation: 566
Right now, you are using index
to get the first customer with the max sales. It sounds like you want to get all the customers that tie for that max sales. There are several ways to do this, but a list comprehension would be especially pythonic:
indices = [sale for sale in sales if sale == salesMax]
Then, to return customers, you could use another list comprehension:
bestCustomers = set(customers[index] for index in indices)
You can read more about list comprehensions here. Notice, the second example isn't a "list" but it uses the same comprehension syntax to create a generator. I fed that into a python set
to make sure it returns distinct customers.
bestCustomer
or salesMax
ahead of time. They can be created on the fly.Upvotes: 2
Reputation: 3600
If your result have multiple same max values you can change your nameOfBestCustomer
function to this one:
def nameOfBestCustomer(sales, customers) :
#@param: sales and customers lists
#@return: none
bestCustomer = ""
salesMax = 0
salesMax = max(sales)
for i in range(len(customers)):
if sales[i] == salesMax:
print("The best customer of the day was " + customers[i] + ".")
print("They spent $%.2f" % salesMax + ".")
Upvotes: 0
Reputation: 11
After finding the maxSale, you can start a loop to find all maximum sales and customers. Also you need to make the bestCustomers a list. Hope this helps.
def nameOfBestCustomer(sales, customers) :
#@param: sales and customers lists
#@return: none
bestCustomers = []
salesMax = 0
salesMax = max(sales)
for i, sale in enumerate(sales):
if sale == salesMax:
bestCustomers.append(customers[i])
print("The best customer of the day was", end='')
for customer in bestCustomers:
print(' ' + customer, end='')
print('.')
Upvotes: 0