Question

Your employer needs a program that analyzes the monthly sales figures for each division. Write a...

Your employer needs a program that analyzes the 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.

Thank you for helping me, my good sir :) I will thumb up lightning speed.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

'''

Python version : 2.7

Python program that analyzes the monthly sales figures for each division.

'''

# function to input and return list of sales amount until user enters negative value

def input_sales():

               sales = []

               amount = 0

              

               while(amount >= 0):

                              amount = float(raw_input('Enter sale amount(-1 to exit) : '))

                              if amount >= 0:

                                             sales.append(amount)

               return sales

# function to return formatted string representing the sales amounts

def formatted_sales(sales):

               salesStr =""

               for i in range(len(sales)):

                              if(i == 0):

                                             salesStr = "{$"+"{:.2f}".format(sales[i])

                              else:

                                             salesStr = salesStr + ", ${:.2f}".format(sales[i])

               salesStr = salesStr + "}"

               return salesStr

# function that determines and returns the maximum sales amount             

def max2(sales):

               if(len(sales) > 0):

                              highest = sales[0]

                              for i in range(1,len(sales)):

                                             if sales[i] > highest:

                                                            highest = sales[i]

                              return highest

               return None

# function that determines and returns the minimum sales amount                             

def min2(sales):

               if(len(sales) > 0):

                              lowest = sales[0]

                              for i in range(1,len(sales)):

                                             if sales[i] < lowest:

                                                            lowest = sales[i]

                              return lowest

               return None       

              

# function that calculates and returns the average sales amount                  

def average(sales):

               if(len(sales) > 0):

                              total = 0

                              for i in range(len(sales)):

                                             total = total + sales[i]

                              return total/len(sales)

               return None       

              

# function that determines and returns the threshold list

def threshold_list(sales, threshold):

               awards_list = []

               for i in range(len(sales)):

                              if sales[i] >= threshold:

                                             awards_list.append(sales[i])

               return awards_list

#main function to test the above functions           

def main():

               sales = input_sales()

               print(' Sales input : '+formatted_sales(sales))

               print(' Highest sale : %.2f' %(max2(sales)))

               print(' Lowest sale : %.2f' %(min2(sales)))

               print(' Average sale : %.2f' %(average(sales)))

               threshold = float(raw_input('Enter the threshold value : '))

               awards_list = threshold_list(sales,threshold)

               print(' Threshold list : '+formatted_sales(awards_list))

#call the mai function     

if __name__ == "__main__":

               main()   

#end of program             

Code Screenshot:

Output:

                                            

              

Add a comment
Know the answer?
Add Answer to:
Your employer needs a program that analyzes the monthly sales figures for each division. Write a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program that allows the user to enter a series of exam scores. The number...

    Write a program that allows the user to enter a series of exam scores. The number of scores the user can enter is not fixed; they can enter any number of scores they want. The exam scores can be either integers or floats. Then, once the user has entered all the scores they want, your program will calculate and print the average of those scores. After printing the average, the program should terminate. You need to use a while loop...

  • LANGUAGE = C i. Write a program that takes int numbers from user until user gives...

    LANGUAGE = C i. Write a program that takes int numbers from user until user gives a sentinel value (loop terminating condition). Sort the numbers in ascending order using Insertion sort method. Sorting part should be done in different function named insertionSort(). Your code should count the number of swaps required to sort the numbers. Print the sorted list and total number of swaps that was required to sort those numbers. (4 marks) ii. In this part take another number...

  • 1) Translate the following equation into a Python assignment statement 2) Write Python code that prints...

    1) Translate the following equation into a Python assignment statement 2) Write Python code that prints PLUS, MINUS, O ZERO, depending on the value stored in a variable named N. 3) What is printed by: 3 - 1 while 5: while 10 Print ) Page 1 of 9 4) Write a Python while loop that reads in integers until the user enters a negative number, then prints the sum of the numbers. 1-2 of 9 4) Write a Python while...

  • 21 Write a program that asks the user to input the length and breadth of a...

    21 Write a program that asks the user to input the length and breadth of a soccer field, and then computes and returns the number of square meters of grass required to cover the field The formula for the area is the product of length and breadth (5) 22 The following program uses the break statement to terminate an infinite while loop to print 5 numbers Rewrite the program to use a while loop to display numbers from 1 to...

  • Total Sales Design a program that asks the user to enter a store’s sales for each...

    Total Sales Design a program that asks the user to enter a store’s sales for each day of the week. The amounts should be stored in an array. Use a loop to calculate the total sales for the week and display the result. Need: variable lists, IPO Chart, pseudocode, Flowchart, and working Python code. 2. Lottery Number Generator Design a program that generates a 7-digit lottery number. The program should have an Integer array with 7 elements. Write a loop...

  • /* I'm trying to write this program here but my problem is getting the program to...

    /* I'm trying to write this program here but my problem is getting the program to save the user input to "int data" but cannot because it is part of the struct. What do I have to do in order to get this to work? (Language is C++) Write a program that prompts the user to enter numbers on the screen and keep adding those numbers to a linked list. The program stops when user enters -999. Hint: Use a...

  • In this assignment you are asked to write a Python program to determine all the prime...

    In this assignment you are asked to write a Python program to determine all the prime numbers in between a range and store them in a list that will be printed when the range is searched. The user is prompted for two positive integer values as input, one is the start of the range and the other is the end of the range. If the user gives a negative value or enters a second number that is lower than the...

  • Complete the missing parts of java source code //This program calculates a running total. Here is...

    Complete the missing parts of java source code //This program calculates a running total. Here is the problem definition //Data Express, an Internet Service Provider, has requested that you develop a program to allow //sales representatives to calculate a running total of their sales based on a number of days. //The program prompts the salesperson “How many days do you have sales figures? “. //The number of days must be between 1 – 4 days (keep asking the user until...

  • Write a Java program to meet the following requirements: 1. Prompt the user to enter three...

    Write a Java program to meet the following requirements: 1. Prompt the user to enter three strings by using nextLine(). Space can be part of the string). ( Note: your program requires using loop: for-loop, while-loop or do-while-loop to get the input String ) 2. Write a method with an input variable (string type).The method should return the number of lowercase letters of the input variable. 3. Get the number of the lowercase letters for each user input string by...

  • use C++ to write the following program. needs to ask the user for n The user...

    use C++ to write the following program. needs to ask the user for n The user must put in those 5 values, probably using a for loop. NOTE: You can assume the number of values to be entered is 5. My attempted program below: (not correct, but just a basis) 4. Larger Than n In a program, write a function that accepts three arguments: an array, the size of the array, and a number n. Assume that the array contains...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT