CLASSWORK--COMPUTER PROGRAMMING 2--Python --------------------------------------------------------------------------- The very first four lines of every program that you write must be labelled with your first and last name, the name of this course, the period of this course, and the date that your program was written (started). All source code must be properly and completely commented and formatted as specified by the instructor. All identifiers should be named according to the procedures outlined by the instructor. Unless a problem states otherwise, programs that you create do NOT need to include error trapping for user input. In other words, it is okay if a program behaves inappropriately due to improper data entered by a user. However, all other aspects of your programs should perform flawlessly. Program output should be grammatically and syntactically correct, neat, presentable, and complete. --------------------------------------------------------------------------- You must write (and compile) all of your Python programs using Python 3.x (not Python 2.x). You may use any editor of your choosing, as long as your submitted programs properly execute using the replit.com online editor. Each program that you write is to be submitted to me online via your Canvas account. --------------------------------------------------------------------------- DUE Friday, April 30, 2021 01) Write a Python 3 program that asks the user how many words he/she would like to enter. Then use a 'for' loop to ask the user to enter each word. After each word is entered, the program should display the word back to the user (with quotation marks around it), along with the number of characters in the word. Your program's output should look exactly like the example output shown below. You do not need to perform error trapping for the user input, and you may put all of your code into the main area of the program (that is, you are not required to create separate functions). This program should not import or use any external functions. Example Program Run ------------------- How many words would you like to enter: 4 Please enter word #1: cat The word is "cat" and it is 3 characters long. Please enter word #2: elephant The word is "elephant" and it is 8 characters long. Please enter word #3: pizza The word is "pizza" and it is 5 characters long. Please enter word #4: automobiles The word is "automobiles" and it is 11 characters long. EXTRA CREDIT OPTION #1 FOR CLASSWORK #1 Do not ask the user how many words will be entered, but instead allow an unlimited number of words. Also, rather than displaying the length of each word as it is entered, wait until the user presses ENTER by itself, and then display back all of the entered words, along with the length of each word. EXTRA CREDIT OPTION #2 FOR CLASSWORK #1 This extra credit option is designed to be added onto the first extra credit option. In addition to displaying the length of each word, also display the frequency of each word in the list (the number of times the word appears in the list, ignoring case). Each unique word in the list should be displayed only one time; do not display the same word (ignoring case) more than once. EXTRA CREDIT OPTION #3 FOR CLASSWORK #1 This extra credit option is designed to be added onto the second extra credit option. Instead of displaying the unique words back to the user in the order in which they were entered, sort the words by their frequency (from most frequent to least frequent), and within that list, sort the words by length. Total Assignment Value: 10 points DUE Wednesday, May 5, 2021 02) Write a Python 3 program that allows the user to enter an unlimited number of words (an empty entry indicates that the user is done typing words). Then display the list of words back to the user in reverse order (the opposite of the order in which they were entered). Make sure your program removes leading and trailing spaces from the user entries and displays the words back to the user in a neat manner, one per line. This program should not import or use any external functions. Example Program Run ------------------- Please enter a word (ENTER by itself to quit): elephant Please enter a word (ENTER by itself to quit): zebra Please enter a word (ENTER by itself to quit): cat Please enter a word (ENTER by itself to quit): porcupine Please enter a word (ENTER by itself to quit): Your words in reverse order: porcupine cat zebra elephant EXTRA CREDIT OPTION FOR CLASSWORK #2 Display a chart showing, in alphabetical order, the letters of the alphabet, along with the number of times that each letter (ignoring case) appears in the complete list of words. For example, if the list contains only the words "kitten", "tiger", and "bear", then, in addition to displaying the words in reverse order, the output should display a chart looking something like: Letter Frequency: A --> 1 B --> 1 E --> 3 G --> 1 I --> 2 K --> 1 N --> 1 R --> 2 T --> 3 Display only the letters being used by the words in the list. Total Assignment Value: 15 points DUE Monday, May 10, 2021 03) Write a Python 3 program that allows the user to enter any number of integers. The user should press ENTER on a line by itself to indicate that he/she is finished entering numbers. Then, the program should compute and report the average, range, and median of the integers entered by the user. If necessary, round computed values to two decimal places. If the user does not enter any numbers, display an appropriate message (instead of computed values). The output from your program should look exactly like the example output shown below. You do not need to perform any error trapping for the user input, and you may put all code into the main area of the program. This program should not import or use any external functions. Example Program Run #1 ---------------------- Please enter an integer (ENTER by itself to quit): 4 Please enter an integer (ENTER by itself to quit): 3 Please enter an integer (ENTER by itself to quit): 5 Please enter an integer (ENTER by itself to quit): 6 Please enter an integer (ENTER by itself to quit): 5 Please enter an integer (ENTER by itself to quit): 5 Please enter an integer (ENTER by itself to quit): 2 Please enter an integer (ENTER by itself to quit): The average of the 7 number(s) that you entered is 4.29. The range of the entered numbers is 4. The median of the numbers is 5. Example Program Run #2 ---------------------- Please enter an integer (ENTER by itself to quit): 17 Please enter an integer (ENTER by itself to quit): -6 Please enter an integer (ENTER by itself to quit): The average of the 2 number(s) that you entered is 5.5. The range of the entered numbers is 23. The median of the numbers is 5.50. Example Program Run #3 ---------------------- Please enter an integer (ENTER by itself to quit): 25 Please enter an integer (ENTER by itself to quit): The average of the 1 number(s) that you entered is 25. The range of the entered numbers is 0. The median of the numbers is 25. Example Program Run #4 ---------------------- Please enter an integer (ENTER by itself to quit): You did not enter any numbers. EXTRA CREDIT OPTION #1 FOR CLASSWORK #3 In addition to computing the average, range, and median of the entered integers, also compute and display the standard deviation of the numbers. EXTRA CREDIT OPTION #2 FOR CLASSWORK #3 Convert each of the (base 10) integers to base 2 (binary). Display the base 2 numbers neatly in a table in the same order in which they were entered by the user. Total Assignment Value: 20 points DUE Friday, May 14, 2021 04) Write a program that first asks the user to enter a word, and then asks the user how many times the word should be randomly scrambled. Your program should then display the word back to the user the given number of times, each time with the letters in the word displayed in a random order. Each scrambled word should appear on a separate line. For this assignment, the 'randint' function is the only function that you may import and use. If you wish, you may put all of your code into the main area of your program (i.e., you are not required to create any separate functions). You also do not need to perform error trapping for any of the user input. Example Program Run ------------------- Please enter a word: animals How many times should the word be scrambled? 10 Here is the word "animals" scrambled 10 times: nalsaim naimsal asmnila ainmlas nailasm msanali iaslanm inamlas lnmiaas asnmali EXTRA CREDIT OPTION #1 FOR CLASSWORK #4 When displaying the list of scrambled words back to the user, make sure no two arrangements of scrambled letters are identical. That is, every scrambled word should be different from every other scrambled word. As a result of this change, there will be a limit to how many letter arrangements there can be for words of varying lengths. For instance, a three-letter word can be scrambled in only six different ways (3 * 2 * 1). A four-letter word can be scrambled in only 24 different ways (4 * 3 * 2 * 1). Therefore, when asking the user how many times the word should be scrambled, provide the user with the maximum value that should be entered. EXTRA CREDIT OPTION #2 FOR CLASSWORK #4 Display the list of scrambled words in alphabetical order. This extra credit option may be implemented before (or instead of) the first extra credit option. Total Assignment Value: 20 points DUE Friday, May 21, 2021 05) A prime number is a positive integer that is divisible only by 1 and itself. Write a program that asks the user how many consecutive prime numbers (starting with 2, which is the smallest prime number) should be displayed. Your program should then calculate and place into a list, in numerical order, the first 'n' prime numbers (where 'n' is the value entered by the user), and then display that list to the user. For example, if the user enters a 5, then your program should display: [2, 3, 5, 7, 11]. For this assignment, you are not permitted to import or use any external functions. If you wish, you may put all of your code into the main area of your program (i.e., you are not required to create any separate functions). However, you DO need to error trap for the user input to make sure the user enters a positive integer greater than zero. Example Program Run ------------------- How many consecutive prime numbers (starting with 2) should be displayed? ten Please enter a positive integer. How many consecutive prime numbers (starting with 2) should be displayed? 0 Please enter a positive integer. How many consecutive prime numbers (starting with 2) should be displayed? -4 Please enter a positive integer. How many consecutive prime numbers (starting with 2) should be displayed? Please enter a positive integer. How many consecutive prime numbers (starting with 2) should be displayed? 27.6 Please enter a positive integer. How many consecutive prime numbers (starting with 2) should be displayed? 15 Here are the first 15 prime numbers: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47] Total Assignment Value: 25 points DUE Friday, June 4, 2021 06) Write a "number guessing" game where the computer randomly generates a positive integer, and then the player is allowed to try to guess the number. After each guess, the computer should tell the player whether the guess was too high, too low, or just right (the correct number). At the start of the program, the player should be prompted to choose the lower and upper boundaries (the range from which the randomly-generated number will be chosen), with a maximum upper boundary of one million. The player should also be asked how many guesses are allowed (up to 20) before the computer reveals the chosen number (if not correctly guessed by the player). All player input should be positive integers, and the upper boundary must be larger than the lower boundary. Be sure to ask for the lower boundary first, then for the upper boundary, and then for the number of allowed guesses. Every time you ask the player to enter a guess, you should also tell the player how many allowed guesses remain. After each game is over, your program should give the player an opportunity to play another round using the same player-chosen values, but with a new computer- generated random number. Your program should error trap for all illegally-formatted player input (i.e., non-integers) and missing data (the player presses ENTER without entering anything). In addition, your program needs to check for all out-of-bounds entries (see above) for all player-inputted values. If such a value is entered, your program should display an appropriate message and the player should be required to re-enter the value. If the player enters an out-of-range guess, make sure that guess does not cause the remaining number of allowed guesses to decrease. In your program you should create a separate function to get the three initial values from the player, a separate function to choose the random number that the player needs to guess, and a separate function with the main game loop (where the player enters guesses and is given feedback). If you wish, all other code may go into the main area of the program. For this assignment, the 'randint' function is the only function that you may import and use. You may use global variables in your program. An example run of my program is shown below. Your output does not need to look exactly like mine, but your program should function in the same manner as my program in accordance with the directions above. Example Program Run ------------------- Welcome to Dave's Number Guessing Game! Please enter a lower boundary (integer greater than zero and less than one million): cat You entered bad data! Please enter a lower boundary (integer greater than zero and less than one million): You did not enter any data! Please enter a lower boundary (integer greater than zero and less than one million): -3 The lower boundary that you entered is out of range! Please enter a lower boundary (integer greater than zero and less than one million): 1000000 The lower boundary that you entered is out of range! Please enter a lower boundary (integer greater than zero and less than one million): 0 The lower boundary that you entered is out of range! Please enter a lower boundary (integer greater than zero and less than one million): 10 Please enter an upper boundary (integer greater than low bound and <= one million): 10 The upper boundary that you entered is out of range! Please enter an upper boundary (integer greater than low bound and <= one million): 1000001 The upper boundary that you entered is out of range! Please enter an upper boundary (integer greater than low bound and <= one million): 14.5 You entered bad data! Please enter an upper boundary (integer greater than low bound and <= one million): 20 Please enter the number of guesses that should be allowed (positive integer <= 20): 21 The guesses allowed that you entered is out of range! Please enter the number of guesses that should be allowed (positive integer <= 20): 0 The guesses allowed that you entered is out of range! Please enter the number of guesses that should be allowed (positive integer <= 20): 3 You have 3 guesses left. Please guess an integer from 10 to 20: 15 Your guess was too low. You have 2 guesses left. Please guess an integer from 10 to 20: 21 Your guess was out-of-bounds. You have 2 guesses left. Please guess an integer from 10 to 20: 9 Your guess was out-of-bounds. You have 2 guesses left. Please guess an integer from 10 to 20: dog You entered bad data! You have 2 guesses left. Please guess an integer from 10 to 20: You did not enter any data! You have 2 guesses left. Please guess an integer from 10 to 20: 17.46 You entered bad data! You have 2 guesses left. Please guess an integer from 10 to 20: 18 Good job...you guessed the chosen number! Would you like to play again with the same parameters (y/n)? y You have 3 guesses left. Please guess an integer from 10 to 20: 15 Your guess was too high. You have 2 guesses left. Please guess an integer from 10 to 20: 13 Your guess was too high. You have 1 guesses left. Please guess an integer from 10 to 20: 11 Your guess was too low. You have run out of guesses. The chosen number was 12. Would you like to play again with the same parameters (y/n)? n Total Assignment Value: 30 points