Description
Create a new directory coms104PA2. Manyhats.com The website manyhats.com sells baseball hats. Hats are $ 10.00 each, or you can get a box of 6 hats for $ 50.00. The shipping charge is $ 2.00 per hat. Example: an order of 15 hats is two boxes of 6, plus 3 singles. It’s 2*50.00 for the boxes plus 3*10.00 for the singles, for a total of 130.00. The shipping is 15*2.00 or 30.00, so the order total is $160.00. Part (A) Write a function called order_total(num) that takes the number of hats (say num) as the input parameter and returns the total cost of an order, including shipping. Save the script as hats.py in coms104PA2 directory. Part (B) Due to the sudden onset of wonderful warm summer weather, suddenly everybody wants a hat. So, manyhats.com decides to offer an overnight shipping service for hats. The hats are now just $10.00 each (no discount for boxes of 6). Instead, of charging $ 2.00 per hat for shipping, overnight shipping is calculated using the following table. Example: An overnight order of 15 hats is 15*10.00 = 150.00 for the hats, plus 45.00 for shipping, so the order total is $195.00. Write a function called order_total_overnight(num) takes the number of hats (say num) as the input parameter and returns the total cost of an order, including shipping using the scenario given in above table. Write this function in the same script hats.py that contains the function defined in Part(A). Important: NO LATE ASSIGNMENT WILL BE ACCEPTED without a valid reason. Feel free to talk to the instructor or a TA if you need any help to complete the assignments. SOLUTIONS SHOULD BE ORIGINAL AND INDEPENDENTLY DEVELOPED. No consultation of other people’s solutions is permitted. No sharing of answers is permitted. Assistance by others (except the instructor or TAs) must be specifically credited in the solution to the problem that is turned in, describing what the contribution was (e.g., “Thanks to [name] for explaining the difference between AND and OR gates in Checkpoint 4”, not “Thanks to [name] for help with Lab Assignment 1.”). Any evidence of plagiarizing will result ZERO points. Please start the programming assignment as soon as possible and get your questions answered early! 2 Part(C) Assume that you have a module called hats.py containing two functions: order_total(num): Returns the total cost for an order of num hats using normal shipping. order_total_overnight(num): Returns the total cost for an order of num hats using overnight shipping. Write a complete script for the user interface to computing the total order cost. A sample interaction when the script is run must look like follows. Item in black font color are response entered by the user. Save this script as “PA2_Question1.py” in coms104PA2 directory. 3 Plus Perfect Numbers (Armstrong number) A plus perfect number is a positive integer that is equal to the sum of its own digits each raised to the power of the number of digits. For example, 371 is a plus perfect number since 3**3 + 7**3 + 1**3 = 371. Write a function called “is_plus_perfect” that takes an integer as the input parameter and return whether it is a plus perfect number or not. For example, find_perfect(371) will return “a plus perfect number” find_perfect(300) will return “not a plus perfect number” Note: Steps of is_plus_perfect function can be given as follows: def is_plus_perfect(n): 1. Define a variable to store the number of digits. Say, digitsN. Initialize digitsN to number of digits in parameter n. (Hint: Convert n to a string value and get the length). 2. Define a variable to store the sum of digits in n each raised to the power of the number of digits. Say, digitPowerSum. Initialize it to zero. 3. For each digit i in n do, Update digitPowerSum such that, digitPowerSum equals to the sum of digitPowerSum in previous iteration and idigitsN. 4. If digitPowerSum equals to n, Return “a plus perfect number” Else, Return “not a plus perfect number” In addition, write Python statements (in the same script) to take a positive integer from user and display whether that number is Perfect or not using the above function. The user interface of your script may look like as follows: Save your script as “PA2_Question2.py”. 4 Matching Pennies (MP) Game Two players simultaneously place a penny on a table. If both pennies show the “Head” Player1 wins the game. If both pennies show the “Tail” Player2 wins the game. Otherwise, the game continues. Suppose we want to write a Python script to simulate this game. Part (A) Write a function called “place_penny” that generate a random number between 1 and 10 and return “Head” if that random number is even and return “Tail” otherwise. Note: This function does not have any input parameter or user inputs. The function randint(a, b) from the random module will generate and return a random number between a and b. For example: from random import randint r = randint(1,10) Then variable r will assign a random number between 1 and 10. Steps of place_penny function can be given as follows. All even numbers are divisible by 2 (i.e. the remainder of number divide by 2 is zero) Import the randint from random module def place_penny (): 5. Generate a random number between 1 and 10 (Say r). 6. If r is even, then return “Head” 7. Otherwise, return “Tail” Save the script as games.py in coms104PA2 directory. Part (B) Write a function called “display_menu” that display following message. ###MENU### p) Play the matching penny game q) Quit Note: This function does not have any input parameter or user inputs. Steps of display_menu function can be given as follows. def display_menu(): 1. Display message ###MENU### 2. Display message p) Play the matching penny game 3. Display message q) Quit Write this function in the same script games.py that contains the function defined in Part(A). 5 Part(C) Assume that you have a module called games.py containing two functions: place_penny(): Return either “Head” or “Tail”. display_menu(): Display the given menu. Write a complete script for the user interface which first displays a menu to the user and allows user to play the game or quit from the program according to the user response. After each round of the game, this menu must be displayed again and the user can choose whether to play again, or quit. Note: Steps of user interface can be given as follows. 1. Import place_penny, display_menu functions from games.py. 2. Call display_menu function. 3. Get user choice (either p or q) 4. While user choice is not equals to “q” do, a. Call place_penny function to get Player1’s penny face. Suppose the returned value is stored in variable pn1. b. Display value of pn1 as “Player1: <>” c. Call place_penny function to get Player2’s penny face. Suppose the returned value is stored in variable pn2. d. Display value of pn2 as “Player2: <>” e. While pn1 is not equals to pn2 do, i. Display message “Game Continue…” ii. Rewrite step a. iii. Rewrite step b. iv. Rewrite step c. v. Rewrite step d. f. If pn1 equals “Head” do, Display “Player 1 wins the game” g. Otherwise do, Display “Player 2 wins the game” h. Call display_menu function. i. Get user choice. (either p or q) 5. Display message Bye!!! 6 A sample interaction when the script is run must look like follows. Item in black font color are response entered by the user. Save this script as “PA2_Question3.py” in coms104PA2 directory. Documentation and style 10% of the points will be for documentation and style. The file should begin with a descriptive comment explaining what it is for Use meaningful names for variables Use helpful prompts when asking the user to enter values Provide a meaningful description as part of the output Do not worry about formatting the number of decimal places in the output. Submission Submit a zip file containing ONLY the files hats.py, PA2_Question1.py, PA2_Question2.py, games.py and PA2_Question3.py via the Programming Assignment 2 Submission link in Blackboard.