## Introduction to Programming (Section 3,4,5 & 6)

Introduction to Programming (Section 3,4,5 & 6)

To get started: 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.
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 “PA2Question1.py” in coms104PA2 directory.
Credit Card Number Check
The last digit of a credit card number is the check digit, which protects against transcription errors such as an error in a single digit or switching two digits. The following method is used to verify actual credit card numbers. For simplicity, in this assignment we assume that the credit card number is only 8 digits.
Suppose the 8 digits credit card number is 43589795
Steps to check the validity:
1. Starting from the leftmost digit, double every other digit. Add all digits of the resulting numbers. For example, with the number given above,
? Every other digit starting from leftmost digit: 4 5 9 9
? Doubling the digits: 8 10 18 18
? Adding all digits in these values: 8+ 1+0+ 1+8+ 1+8 = 27
2. Get the sum of each of the digits that were not included in the preceding step. For example, with the number given above,
? Digits not included: 3 8 7 5
? Sum: 3+8+7+5 = 23
3. Add the sums of the two preceding steps. If the last digit of the result is 0, the number is valid. In above case, 27+23 = 50, so the number is valid.
Write a Python function called “validateCreditCard” that takes 8-digit credit card number as the input parameter (string value) and display whether that number is valid or not. For example,
validateCreditCard(“43589795”) will display “Valid credit card number”
validateCreditCard(“43589792”) will display “Invalid credit card number”
Note:
? Steps of validateCreditCard function can be given as follows:
3
def validateCreditCard(card):
1. Define a variable to store double digits and initialize to empty string. Say, double_digits = “”
2. Define a variable to store the sum of double digits and initialize to zero. Say, double_digit_sum = 0
3. Define a variable to store the sum of remaining digits and initialize to zero. Say, remaning_sum = 0
4. For each number i in range(0,8) do,
a. Convert card character at index i to integer. Say, int_digit
b. If i is divisible by 2,
i. Double the int_digit. Say double_int_digit. (That is, double_int_digit = int_digit*2)
ii. Convert double_int_digit to a string value and concatenate to double_digits (That is, double_digits += str(double_int_digit)
c. Otherwise,
i. Convert card character at index i to integer. Say, int_digit
ii. Add the int_digit to remaning_sum
5. For each character c in double_digits do,
a. Convert c to integer. Say int_c
6. Add the values of double_digit_sum and remaning_sum together and store in a variable. Say, steps_total.
7. Convert steps_total in to a string value. Say steps_total_str
8. If steps_total_str ends with substring “0”,
a. Display “Valid credit card number”
9. Otherwise,
a. Display “Invalid credit card number”
In addition, write Python statements (in the same script) to take 8 digits from user as a string value (use raw_input function) and display whether that it is valid or not. If user enters less than or greater than 8 digits, display an error message.
A sample interaction when the script is run must look like follows. Item in black font color are response entered by the user.
Case 1:
Case 2:
Case 3:
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 ():
10. Generate a random number between 1 and 10 (Say r).
11. If r is even, then return “Head”
12. Otherwise, return “Tail”
Save the script as “games.py” in coms104PA2 directory.
Part (B)
Write a function called “display_menu” that display following message.
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.
2. Display message p) Play the matching penny game
3. Display message q) Quit
5
Write this function in the same script games.py that contains the function defined in Part(A).
Part(C)
Assume that you have a module called games.py containing two functions:
place_penny(): Return either “Head” or “Tail”.
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.
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: <<pn1>>”
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: <<pn2>>”
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”
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 “PA2Question3.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