0

This is my first time asking a question here, and I am new to programming so be gentle. I am attempting to create a code in which the user enters sales for each day of the week and the functions display the sales for each day and display the day with the highest sales/average sales/act.

The intended result would be something like:

Mondays sales: $200
Tuesdays sales: $100
Wednesday sales: $50
Thursday sales: $75
Friday sales: $100
Saturday sales: $250
Sunday sales: $75
Highest sales achieved on Saturday with $250

However, now the results are being printed 7 times each and I do not know how to reference the max value with the related day

Here is what I have so far:

days_week=["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]

def fillList(salesList):
    for index in range(len(days_week)):
        user_input=int(input("Enter sales for "+ days_week[index]+": " ))
        salesList.append(user_input)


def showSales(salesList):
    for sales in salesList:
        for index in range(len(days_week)):
                print(days_week[index],"sales: $", sales)

def highestSales(salesList):
    print("Highest sales of the week was achieved on",max(salesList))



def main():
    salesList=[]
    fillList(salesList)
    showSales(salesList)
    highestSales(salesList)

main()

After making the recommended changes, the program now "works," displaying the day's sales as intended.

days_week=["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]

def fillList(salesList):
    for day in days_week:
        user_input=int(input("Enter sales for "+ day+": " ))
        salesList.append(user_input)

def showSales(salesList):
    for num in range(len(days_week)):
        print(days_week[num],"sales: $", salesList[num])

def highestSales(salesList):
    print("Highest sales of the week was achieved on",max(salesList))

def main():
    salesList=[]
    fillList(salesList)
    showSales(salesList)
    highestSales(salesList)

main()    

However, I am trying to display the day in which the most sales occured. Is it possible to append the two lists together? Or is there a simple way of displaying the day with the largest user input?

Jnoe
  • 11
  • 3
  • 1
    In `showSales`, you have a loop within a loop. The outer loop iterates over the sales list (there are 7 of them) and the inner loop iterates over the days of the week (and there are 7 of those). So it will print 7x7 times. – jarmod Nov 16 '19 at 00:35

3 Answers3

0

You are have an extra for loop in your showSales(saleslist). I presume you only have the inner for loop to get the index that you want for days_week.

The simplest fix to this is to use a counter while in your for loop, something like:

def showSales(salesList):
    i = 0;
    for sales in salesList:
        print(days_week[i],"sales: $", sales)
        i += 1

Python also has a built in function for this use case, enumerate. Enumerate returns the counter and your iterator, like so:

def showSales(salesList):
    for i , sales in enumerate(salesList):
        print(days_week[i],"sales: $", sales)

Though the first method is universal across languages, and will give you more clarity when learning - so I recommend explicitly using a counter.

waffles
  • 208
  • 2
  • 11
  • That is a very clever workaround I hadnt thought of how to get the index for days_week. Exactly what I was looking for. Thank you for the help. – Jnoe Nov 16 '19 at 00:44
0

Think carefully about what your loops are doing. Per your code, each item in the first list is being iterated over 7 times, then Python moves to the next item and repeats that 7 times, hence you are getting each result printed 7 times.

For the function showSales, try something like this:

def showSales(salesList):
    for num in range(len(days_week)):
        print(days_week[num],"sales: $", salesList[num])

As a side note, you can improve fillList as you don't need to create a range object for a list, it is already iterable. For example:

def fillList(salesList):
    for day in days_week:
        user_input=int(input("Enter sales for "+ day+": " ))
        salesList.append(user_input)

Now you will only loop through each day once.

schaefferda
  • 338
  • 1
  • 4
  • 14
  • Wow, great help. You must be an instructor or something. Your advice worked perfectly. Thank you, I have a much better understanding now. – Jnoe Nov 16 '19 at 00:42
  • Only thing I am still unsure with is how to directly correlate the two lists when trying to print the day with the highest sales – Jnoe Nov 16 '19 at 00:48
  • @Jnoe, you may want to consider using a dictionary rather than lists if your problem allows for it. Consider a dictionary with key:value pairs of {monday: 200, tuesday: 100, ...}, then you can reference the value by the key rather than having two separate lists. I think this might help with your correlation issue you mentioned. – schaefferda Nov 20 '19 at 18:09
0

Welcome to Stack Overflow @Jnoe.

So you have two questions.

First: Why the result is being printed 7 times?

As people mentioned you have two nested for's statements in showSales(salesList). To get the behavior I believe you want, you can remove the inner for and take advantage of the fact that each sale have the same index in the sales list as the day in wich the sales hapened has in the days_week list. To get this index you can use the list.index(value) function. This function returns the index of value in the list.

So, to clarify, in your example you have that:

salesList.index(75) will return 6 and days_week[6] is sunday, the day the 75 sale happened.

So your code should be like this:

def showSales(salesList):
    for sales in salesList:
        print(days_week[salesList.index(sales)],"sales: $", sales)

Second: How to reference the max value with the related day?

You should probably know the answer by now. You can use the same function used previously, but now to find the index in wich the max value ocurred in the sales list.

So your code should be something like this:

def highestSales(salesList):
    print("Highest sales of the week was achieved on", days_week[salesList.index(max(salesList))])

This means get the max value out of salesList, then get the index of salesList in wich this value happened, now get the days_week of this index.

Be careful!

Because list.index(value) returns the index of the first time the value you passed as argument appears in the list, if you have the same sale in different days you would probably get an undesirable behavior.

To adapt to that you could use a list comprehension but I believe it is out of the scope of your question.

  • I was unaware or the .index function and that was a real life saver. Your advice worked just as intended. You are very knowledgeable indeed, thank you. – Jnoe Nov 16 '19 at 01:25