FA23-CSC121-Lab05

docx

School

Wake Tech *

*We aren’t endorsed by this school

Course

121

Subject

Computer Science

Date

Apr 3, 2024

Type

docx

Pages

7

Uploaded by ColonelFlower887

Report
CSC121 Python Programming Revised 8/5/2023 CSC121 L AB 05: F UNCTIONS P ART 2; L ISTS P ART 1 G OALS In this lab assignment, students will demonstrate the ability to: Send and receive multiple return values in functions Create and use functions in modules Create a list Access list elements Alter a list Iterate over a list Do list copying, concatenation, and slicing Use operators, list methods, and built-in functions on lists NOTE: This lab document is a copyrighted work of Wake Tech Community College and the course instructor. Any posting of this document outside of Wake Tech is considered a copyright violation. Students who post these documents outside of Wake Tech are subject to academic and possible legal actions. I NSTRUCTIONS In this lab, you will demonstrate your understanding of value-returning functions. You will also demonstrate your understanding what we've learned about lists so far. Follow the instructions in each problem and submit the specified files. All problems will require that Python code be submitted as well as screenshots that prove the programs have been executed in PyCharm. Problems 1 includes code provided by the instructor that needs to be completed. Problem 2 consists of a program that you create from scratch that meets the problem specification. P ROBLEMS P ROBLEM 1 Trish at Bargain Swap Shop continues to use the programs you developed for her. On the previous program you did for Lab 3, Trish wants to update the program so that it has a nicer receipt. She also wants to include a 5% discount for all customers who buy more than a certain amount of a particular item. The main function: As with Lesson 4 as well as future lessons, all programs that you write from scratch must have a main function that contains the top-level logic of your code. Otherwise, you should follow the instructions provided in the lab document.
______________________________________________________________________________________________________________________________________________________________________________________________________________________ CSC121 Lab 05 Page 2 ______________________________________________________________________________________________________________________________________________________________________________________________________________________ You expect that Trish will continue to ask you to extend the types of items that she currently sells. So, you've decided to also update the program you did for Lab 03 to use functions. The instructor has provided two files called Lab05P1.py and itemProcessor- FillThisIn.py . Download those files and rename the second file itemProcessor.py . Copy those files into your PyCharm project. Change the program headers to include your name and the date. The partial program is made up of 4 functions in two modules (files): main in Lab05P1.py - DO NOT CHANGE ANY OF THE CODE IN THIS FILE. This function is already complete. It imports the helper module and calls the other 3 functions to accomplish its work: The main function calls get_item_count and then get_item_total 3 times to get the item costs for books, DVDs, and games. o Notice that the main program passes in the parameters that determine the item unit price, maximum number of items allowed, and the threshold for the discount. Then the main function calls calc_and_display_receipt to print the receipt. get_item_count in itemProcessor.py This function will need to be implemented. This function takes three parameters: o item_name – name of the item being prompted about o max_allowed – maximum number of items the user can enter o discount_threshold – minimum number of items eligible for a 5% discount The function will ask the user to enter the number of items using a prompt which contains the item name passed in as a parameter. If the user enters a value less than 0 or more than the max allowed, an error message will be printed, and they will be asked to enter a different value. See the sample output for an example. The function will return the number of items the user enters. get_item_total in itemProcessor.py This function will need to be implemented. This function takes three parameters: o num_items – number of items o unit_price – the cost of each item o discount_threshold – minimum number of items eligible for a 5% discount The function calculates the total cost for the items. If the number of items is greater than or equal to the discount threshold, the total will be discounted by 5%. The function returns the total cost of all the items including the discount if applicable. BOOK_MAX = 40 DVD_MAX = 15 GAME_MAX = 25 BOOK_DISCOUNT = 5 DVD_DISCOUNT = 8 GAME_DISCOUNT = 4 Revised 8/5/2023
______________________________________________________________________________________________________________________________________________________________________________________________________________________ CSC121 Lab 05 Page 3 ______________________________________________________________________________________________________________________________________________________________________________________________________________________ calc_and_display_receipt in itemProcessor.py This function will need to be implemented. This function takes three parameters: o books_total – total cost of books o dvds_total – total cost of DVDs o games_total – total cost of games The function will calculate the total before tax. The function will calculate the sales tax using the function you created. Use the provided global constant for the sales tax. The function will calculate the total after tax. Then the function will print a receipt that contains the following information: o Total cost of books ( if zero , skip this step) o Total cost of DVDs ( if zero , skip this step) o Total cost of games (if zero , skip this step) o A divider line o Subtotal before tax o Amount of tax to be charged o Total including tax Here's some sample outputs of running the program: Enter the number of books. 5 or more receive a discount: 8 Enter the number of DVDs. 8 or more receive a discount: 3 Enter the number of games. 4 or more receive a discount: 4 Books: $17.10 DVDs: $13.05 Games: $19.00 --------------------- Subtotal: 49.15 Tax: 3.19 Amount due: 52.34 Revised 8/5/2023
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
______________________________________________________________________________________________________________________________________________________________________________________________________________________ CSC121 Lab 05 Page 4 ______________________________________________________________________________________________________________________________________________________________________________________________________________________ Enter the number of books. 5 or more receive a discount: -5 Number of items must be between 0 and 40 Enter the number of books: 3 Enter the number of DVDs. 8 or more receive a discount: 0 Enter the number of games. 4 or more receive a discount: -2 Number of items must be between 0 and 25 Enter the number of games: 40 Number of items must be between 0 and 25 Enter the number of games: 4 Books: $6.75 Games: $19.00 --------------------- Subtotal: 25.75 Tax: 1.67 Amount due: 27.42 NOTE: We are not handling issues where a user types an invalid integer, that is, an entry which cannot be converted using the int() function. We will handle that in a future lab. Run this program. Take a screenshot of the results. Name the screenshot Lab05P1-ouput.jpg . Submit all 3 files, Lab05P1.py , itemProcessor.py , and Lab05P1-output.jpg , to Blackboard for credit. P ROBLEM 2 In this problem, you will create a program from scratch based to demonstrate your abilities to create and manipulate lists in Python. Create a file named Lab05P2.py . For this program: Label each block of code that performs the different steps with an appropriate comment (e.g. # Step a). Your output should also include indications as to what step is being displayed. See the Sample Output for an example. Remember to put your program in a main function and call main at the end of your file. Write a Python program that performs the following steps: a) Ask the user for the lower and upper bound for generating random numbers. b) Ask the user how many numbers they want in the lists. This number will determine the size of BOTH lists. c) Use a for loop and a random integer generator to generate random integers from lower to upper bounds the user specified in step a. This is an inclusive range. The program should generate the number of integers indicated by the user in step b. Store the random integers in a list. Display the list. d) Create a second list in the same way as was done in step b. Display the second list. Revised 8/5/2023
______________________________________________________________________________________________________________________________________________________________________________________________________________________ CSC121 Lab 05 Page 5 ______________________________________________________________________________________________________________________________________________________________________________________________________________________ NOTE: You could create a function that generates a list like what is specified in steps c and d. To do that, pass in the numbers from steps a and b as parameters, and then return the list that is generated. e) Use a for loop to display the elements in the two lists in pairs, i.e., display the first elements in both lists, display the second elements in the both lists, etc. HINT: Have your for loop increment through the lists using an index. That will allow you to access the 0 th elements, then the 1 st elements, etc. f) Combine the lists into a single list and display the combined list. g) Sort the combined list in place and display the sorted combined. Do NOT use the sorted function. Use the method we learned about in class. h) Use list slicing to display the first three elements in the list and the last three elements in the list. i) Display the total of the elements in the list. Use a single function to calculate that total. j) Display the minimum element in the list. Use a single function to determine that element. k) Display the maximum element in the list. Use a single function to determine that element. l) Use a for loop and a random integer generator to generate 5 random integers in the range specified by the user in step a. Check if each number is in the combined list. If the number is in the combined list, print the number and print the index of the first element that matches. NOTE: There is a list method for finding an element value which return the index and you should use that function. Do NOT use a for loop or you will lose some points. If the element is found, after printing the index, the element should be deleted or removed from the list. NOTE: There is both a function and a list method that can delete or remove an element from a list. You should use either the function or the list method. Do NOT use a for loop or you will lose some points. If the number is not in the combined list, print the number followed by "not found in list". m) Print the combined list now that elements may have been deleted. Sample output: Revised 8/5/2023
______________________________________________________________________________________________________________________________________________________________________________________________________________________ CSC121 Lab 05 Page 6 ______________________________________________________________________________________________________________________________________________________________________________________________________________________ Step a: How many numbers in each list? 5 Step b: What is the lower bound for the random number? 1 What is the upper bound for the random numbers? 15 Step c: First list: [13, 8, 5, 14, 13] Step d: Second list: [6, 9, 15, 5, 3] Step e: 13 6 8 9 5 15 14 5 13 3 Step f: Combined list: [13, 8, 5, 14, 13, 6, 9, 15, 5, 3] Step g: Sorted list: [3, 5, 5, 6, 8, 9, 13, 13, 14, 15] Step h: First three elements: [3, 5, 5] Last three elements: [13, 14, 15] Step i: Sum: 91 Step j: Minimum: 3 Step k: Maximum: 15 Step h: 8 at index 4 3 at index 0 11 not found in list 14 at index 6 7 not found in list Step m: Final list: [5, 5, 6, 9, 13, 13, 15] Run this program using the PyCharm Terminal. Take a screenshot of the Terminal that includes the line showing where you started the program run with the results. Name the screenshot Lab05P2-ouput.jpg . Submit both files, Lab05P2.py and Lab05P2-output.jpg , to Blackboard for credit. G RADING R UBRIC G RADING RUBRIC FOR P ROBLEM 1 (45 POINTS ) Program has a well-formatted and correct header [5 points] Program does execute correctly and produces correct results [35 points] Screenshots demonstrates student executed the faulty program and the corrected program [5 points] G RADING RUBRIC FOR P ROBLEM 2 (55 POINTS ) Program has a well-formatted and correct header [5 points] Program does execute correctly and produces correct results [45 points] Screenshot demonstrates student executed the program [5 points] Revised 8/5/2023
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
______________________________________________________________________________________________________________________________________________________________________________________________________________________ CSC121 Lab 05 Page 7 ______________________________________________________________________________________________________________________________________________________________________________________________________________________ Revised 8/5/2023