Answered step by step
Verified Expert Solution
Question
1 Approved Answer
here is the code the last four pages are the code 5 points Status: Not Submitted 3 . Computer Programming Unit 8 Lab 2 In
here is the code
the last four pages are the code
5 points Status: Not Submitted 3 . Computer Programming Unit 8 Lab 2 In this lab, you will create a program that will create lists of numbers and utilize functions for lists. The tasks a user can perform will be organized in a menu. The program will perform the following tasks: Create a list of perfect squares. The user will select how many perfect squares they want to find. Create a list of prime numbers. The user will select how many prime numbers they want to find. Combine the list above into one list. To do this, first make a copy of one of the lists and then use the appropriate list function to combine them. See below for help on copying lists. Sort the list of all numbers created in the previous bullet. Clear any of the lists you created. . Use the functions called printMenu() and testPrime() detailed in the given code. Required Python elements: Variables used for inputs, outputs and other values needed for computations. Proper naming of the variables . Required Python elements: Variables used for inputs, outputs and other values needed for computations. Proper naming of the variables . Using the input function to get the data Proper use of mathematical operators Concatenation of string and integer types Create and use functions as needed. Create and use lists as needed. Coding Help: When creating programs with menus, the typical algorithm is as follows: Print the Menu Get the user's choice while the user didn't choose quit if the user chose option 1 do the work for option 1 Else if the chose option 2 do the work for option 2 Print the Menu Get the user's choice RUN CODE I TEST CASES ASSIGNMENT DOCS GRADE | MORE Print the Menu Get the user's choice Use the function, printMenu() to print the menu and get the user input for their menu choice. This value should then be returned from the function. So in the main program, when you want to display the menu and get the user's choice, you will just have a line of code that looks like this: choice = printMenu() Use the function, testPrime().to see if the number given as a parameter (num) is a prime number. The function should return True if it is and False otherwise. . There are a couple of different ways you can write this algorithm. Since the function will return a boolean, it can be used as the condition for an if statement if (testPrime (numberToTest)): . Get this program working one menu option at a time. To create a new empty list, just let the list equal empty brackets: newlist - 01 To copy lists, you need to create a new list and then in a loop, add one element at a time into the new list. Example: newList - 0) for i in oldList: newList.append(1) You must use the "Press Enter to continue" to properly pass the autograder. Use the following line of You must use the "Press Enter to continue" to properly pass the autograder. Use the following line of code at the bottom of your while loop to make this work: input("Press Enter to continue...") Example output: Choose an option: 1: Create a List of Perfect Squares 2: Create a list of Prime Numbers 3: Combine the Lists into one List 4: Sort a List 5: Clear a List 8: Quit >>> 1 How many perfect squares do you want to find? 4 [1, 4, 9, 16] Press Enter to continue... Choose an option: 1: Create a list of Perfect Squares 2: Create a list of Prime Numbers 3: Combine the Lists into one List 4: Sort a List 5: Clear a List RUN CODE TEST CASES ASSIGNMENT autograder. Use the following line of code at the bottom of while loop to make this work: input("Press Enter to continue.. Example output: Choose an option: 1: Create a List of Perfect Squares 2: Create a List of Prime Numbers 3: Combine the Lists into one List 4: Sort a List 5: Clear a List 0: Quit >>> 1 How many perfect squares do you want to find? 4 [1, 4, 9, 16] Press Enter to continue... RUN CODE TEST CASES ASSIGNMENT DOCS GRADE | MORE Choose an option: 1: Create a list of Perfect Squares 2: Create a List of Prime Numbers 3: Combine the Lists into one List 4: Sort a List 5: Clear a List : Quit >>> 2 How many prime numbers do you want to find? 6 [2, 3, 5, 7, 11, 13] Press Enter to continue... Choose an option: 1: Create a List of Perfect Squares 2: Create a list of Prime Numbers 3: Combine the Lists into one List 4: Sort a List 5: Clear a List a: Quit > >> 3 [1, 4, 9, 16, 2, 3, 5, 7, 11, 13] Press Enter to continue... RUN CODE T TEST CASES ASSIGNMENT DOCS GRADE MORE [1, 4, 9, 16, 2, 3, 5, 7, 11, 13] Press Enter to continue... Choose an option: 1: Create a List of Perfect Squares 2: Create a List of Prime Numbers 3: Combine the Lists into one List 4: Sort a List 5: Clear a List 8: Quit All the values sorted: [1, 2, 3, 4, 5, 7, 9, 11, 13, 16] Press Enter to continue... Choose an option: 1: Create a list of Perfect Squares 2: Create a list of Prime Numbers 3: Combine the Lists into one List 4: Sort a List 5: Clear a List @: Quit ASSIGNMENT DOCS GRADE MORE RUN CODE T TEST CASES 3: Combine the Lists into one List 4: Sort a List 5: Clear a List : Quit >>> 5 Which list do you want to clear? 1: Perfect Squares 2: Prime Numbers 3: All Numbers Perfect squares list has been cleared. Press Enter to continue... Choose an option: 1: Create a list of Perfect Squares 2: Create a list of Prime Numbers 3: Combine the Lists into one List 4: Sort a List 5: Clear a List 8: Quit Good bye Lab 08-02 Lists of Values Save Submit + Continue 1 import math 2 3 4 5 # Functions 7 # Print the menu and return the user's choice 8- def printMenu(): 9 print ("Choose an option:") 10 print ("1: Create a List of Perfect Squares") 11 print ("2: Create a list of Prime Numbers") 12 print ("3: Combine the Lists into one List") 13 print ("4: Sort the combined List") 14 print ("5: Clear a List") 15 print ("e: Quit") 16 choice = int(input(">>> ")) 17 - while (choice 5): print ("Invalid choice") 19 choice = int(input(">>> ")) 20 return choice 21 # Tests num to see if it is prime numbers. Returns True if it is prime 23. def testPrime(num): isPrime = True 25 for i in range (2, num): 26 if (num % i *): 27 Lima 18 22 24 rola Lab 08-02 Lists of Values Save Submit Continue # F# # 26 if (num% i == 0): 27 isPrime = False 28 return isPrime 29 30 #=== 31 32 33 Main Program 34 35 36 perfectSquares = [] # List for storing the perfect squares 37 primeNumbers [ # List for storing the prime numbers 38 allNumbers = [] # List for storing the the combined lists (per 39 # Print the menu and get the user's choice 41 choice = printMenu() 42 print 43 44 # Keep going untilt he user quits 45 - while (choice != 0): 46 47 # User has chosen to create a list of perfect squares. Asks the us # many they want and adds that many perfect squares to the list. 49 - if (choice == 1): 50 51 COMPLETE THIS SECTION TO ADD PERECT SQUARES TO THE LIST 52 40 48 NA Lab 08-02 Lists of Values Save Submit + Continue # ---> COMPLETE THIS SECTION TO ADD PERECT SQUARES TO THE LIST 51 52 53 54 55 print perfectSquares # User has chosen to create a list of prime numbers. Asks the user # many they want and adds that many prime numbers to the list elif (choice == 2): 56 # ---> COMPLETE THIS SECTION TO ADD PRIME NUMBERS TO THE LIST 57 - 58 59 60 61 62 63 64- 65 - 66 67 - 68 69 print primeNumbers # User has chosen to combine the lists. Displays the lists combine elif (choice == 3): if (len(perfectSquares) == 0 or len(primeNumbers) == 0):| print ("Please create both Perfect Square and Prime Number else: COMPLETE THIS SECTION TO COMBINE THE PRIME NUMBER LIST TO OF THE PERFECT SQUARE LIST 70 print allNumbers 71 72 73 74 75 76 77 # User has chosen to sort the combined list. Displays the sorted 1 elif (choice 4): if (len (perfectSquares) == or len(primeNumbers) O or le - == @ Lab 08-02 Lists of Values Save Submit + Continue 77 78 - 74 # User has chosen to sort the combined list. Displays the sorted 75 elif (choice == 4): 76 - if (len(perfectSquares) == 0 or len(primeNumbers) == 0 or len print ("Please create both Perfect Square and Prime Numbe else: 79 80 # ---> COMPLETE THIS SECTION TO SORT THE LIST OF COMBINED NUMBER: 81 82 print allNumbers 83 84 # User has chosen to clear one of the lists. They choose which li 85 # and then that list gets erased. 86 elif (choice 5): 87 print ("Which list do you want to clear?") 88 print ("1: Perfect Squares") 89 print ("2: Prime Numbers") 90 print ("3: All Numbers") 91 listToCount = int(input(">>>")) 92 - if (listToCount == 1): 93 94 # ---> COMPLETE THIS SECTION TO CLEAR THE PERFECT SQUARE LIST 95 96 print ("Perfect squares list has been cleared.") 97- elif (listToCount 2): 98 99 # ---> COMPLETE THIS SECTION TO CLEAR THE PERFECT SQUARE LIST 100 == Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started