Login
Order Now
Support
theprogrammingassignmenthelp

Python Program That Analyzes Monthly Sales Figures For Each Division.

  • 12th May, 2019
  • 13:55 PM

Challenge - Python Program That Analyzes Monthly Sales Figures For Each Division.

Write a Python program that allows the user to enter a series of numbers and places the numbers (not string values) in a list.  Each number entered by the user is the monthly sales amount for one division.  Use a sentinel-controlled loop to get input from the user and place each value entered into the list as long as the value is not negative.  As soon as any negative value is entered, stop the loop (without putting that illegal value into the list).

One objective of this assignment is to apply algorithms we have studied to solve problems. You will do this by writing a function definition to implement each algorithm.  Every function must get information only from its parameters, not from "global" variables.  Several of these tasks can also be accomplished with built-in functions.  In those cases, your code should demonstrate both approaches and show that the results are the same.

Add code to your program to do the following:

  1. Write a function definition that uses a loop and a string accumulator to produce and return a string result that includes the user-entered sales figures formatted to look similar to this when it is printed:
    {$12.95, $1,234.56, $100.00, $20.50}
    Notice the dollar signs, commas, digits after the decimal point, and curly braces.  Plan this one on paper before you start writing code.  Add code that calls this function and prints the result.
  2. Show the highest number in the list -- the sales leader! Do this in two ways. First, use the built-in 'max' function. Then, write your own 'max2' function definition that accomplishes the same thing by using a loop to find the highest value.
  3. Show the lowest number in the list -- the sales loser. Do this in two ways. First, use the built-in 'min' function. Then, write your own 'min2' function definition that accomplishes the same thing by using a loop to find the lowest value.
  4. Show the total sales for the company for the month -- the sum of all the numbers in the list. Once again, write a function definition that uses a loop and an accumulator to compute this sum.
  5. Show the average of all the numbers in the list. (Be sure to carefully check the result!)
  6. Ask the user to enter a threshold (a number). Write a function that takes a list and the threshold as parameters, and returns a new list that contains all values in the original list that are greater than or equal to the threshold -- these divisions get awards for high sales! The new list may, of course, be empty.

 

Python Program

def display(sales):
    sales2=['']*len(sales)
    for i in range(0,len(sales)):
        sales2[i]='$' + '{:,.2f}'.format(sales[i])
    return '{' + ', '.join(sales2) + '}'

def max2(sales):
    if len(sales)==0:
        return None
    m = sales[0]
    for sale in sales[1:]:
        if sale > m:
            m = sale
    return m

def min2(sales):
    if len(sales)==0:
        return None
    m = sales[0]
    for sale in sales[1:]:
        if sale < m:
            m = sale
    return m

def sum2(sales):
    s = 0
    for sale in sales:
        s += sale
    return s

def average(sales):
    if len(sales)==0:
        return None
    s = sum2(sales)
    return s / len(sales)

def thresh (sales, threshold):
    return [sale for sale in sales if sale >= threshold]

def main():

    sales = []
    while True:
        sale = float (input ('Please input the number to add and enter -1 to stop: '))
        if sale == -1:
            break
        sales.append(sale)

    print('')
    print (display(sales))
    print('')
    print ("Max using max function :"+str(max(sales)))
    print ("Max using max2 function :"+str(max2(sales)))

    print ('')
    print ("Min using min function :"+str(min(sales)))
    print ("Min using min2 function :"+str(min2(sales)))

    print ('')
    print ("Sum using sum function :"+str(sum(sales)))
    print ("Sum using sum2 function :"+str(sum2(sales)))

    print ('')
    print ("Average :"+str(average(sales)))

    print ("")
    threshold = float (input ('Please input a thereshold: '))
    sales = thresh (sales, threshold)
    print ("result: " + display(sales))

main()

 

Share this post