+1 678 648 4277 

Function and List processing using Python Assignment solution


Understanding List Processing

Assignment:

Write Python code that exercises your understanding of functions and lists in Python. You can build your code one function at a time, test it, and move to the next part.

Constraints:

  • All functions are run through the main function
  • Define your functions first and then call functions as required
  • Feel free to use any predefined functions
  • Do not use any global variables
  • Avoid using break and continue statements
  • Avoid using exit()

Here is the skeleton of a sample code that you may want to follow:

# display of menu of choices def menu():

#process test scores – return average def procTest (test):

#process as et of purchases def processPurchases (num):

#process even numbers

def even_numbers (n1, n2):

#the main function def main():

# call the functions to process the data

##MAKE sure to define your lists for the names and votes in your main function

main() ### call the main to start the process

Sample test run 1 – Make sure to fully test your code. Make sure to re-create all the output shown in the sample runs. Don’t worry about lining up the output.

Enter 1 to process test scores

Enter 2 to process purchase prices

Enter 3 to process even numbers

Enter 4 to exit

42

Invalid choice - try again

Enter 1 to process test scores

Enter 2 to process purchase prices

 Enter 3 to process even numbers

Enter 4 to exit


1

Please enter the test scores. Type -1 when finished: 100

Please enter the test scores. Type -1 when finished: 80

Please enter the test scores. Type -1 when finished: 90

Please enter the test scores. Type -1 when finished: 85

Please enter the test scores. Type -1 when finished: -1

Total: 355.00

Lowest: 80.00

Highest: 100.00

Average: 88.75

Enter 1 to process test scores

Enter 2 to process purchase prices

Enter 3 to process even numbers

Enter 4 to exit

2

Enter the number of products to purchase: 2

Enter the price of an item: 100.76

Enter the price of an item: 10

Average price of all products: 55.38

Enter 1 to process test scores

Enter 2 to process purchase prices

Enter 3 to process even numbers

Enter 4 to exit

3

Enter the first number: 8

Enter the second number: 20 8

10

12

14

16

18

20

Total of all even numbers = 98 Enter 1 to process test scores

Enter 2 to process purchase prices

Enter 3 to process even numbers

Enter 4 to exit

4

Solution:

# display of menu of choices def menu(): print("Enter 1 to process test scores") print("Enter 2 to process purchase prices") print("Enter 3 to process even numbers") print("Enter 4 to exit") #process test scores – return average def procTest (test): print(f"Total: {sum(test)}") print(f"Lowest: {min(test)}") print(f"Highest: {max(test)}") print(f"Average: {sum(test)/len(test)}") #process as et of purchases def processPurchases (num): price_values = [] for i in range(num): price = float(input("Enter the price of an item: ")) price_values.append(price) avg = sum(price_values)/len(price_values) print(f"Average price of all products: {avg}") #process even numbers def even_numbers (n1, n2): if n1 <= n2: total = 0 for num in range(n1, n2+1): if num%2 == 0: total += num print(num) print(f"Total of all even numbers = {total}") else: print(f"Total of all even numbers = {-100}") #the main function def main(): # call the functions to process the data ##MAKE sure to define your lists for the names and votes in your main function choice = "inf" while choice != "4": choice = "inf" menu() while(choice not in ["1","2","3","4"]): choice = input() if choice not in ["1","2","3","4"]: print("Invalid choice - try again") if choice == "1": scores = [] score = "" while score != "-1": score = input("Please enter the test scores. Type -1 when finished: ") if score != "-1": scores.append(float(score)) procTest(scores) elif choice == "2": num = -1 while int(num) < 0: num = input("Enter the number of products to purchase:") if int(num) >0: processPurchases (int(num)) else: print("Invalid. Number has to be positive integer.") elif choice == "3": n1 = int(input("Enter the first number:")) n2 = int(input("Enter the second number:")) even_numbers (n1, n2) main() # In[ ]: