In: Computer Science
IN PYTHON
Your task this week is to use loops to both validate user input as well as automate a test suite for your lab solution from last week.
Your program will prompt the user for the cost of their groceries.
Then you are to calculate the value of the coupon to be awarded based on the amount of the grocery bill. Use the table below to determine coupon amounts.
Money Spent | Coupon Percentage |
Less than $10 | 0% |
From $10 to less than $60 | 8% |
From $60 to less than $150 | 10% |
From $150 to less than $210 | 12% |
From $210 or more | 14% |
Display an appropriate result message to the user including both the coupon percentage awarded and the dollar amount of their discount coupon.
Last week we trusted the user to provide valid input. This week you will ensure that valid user input is obtained by implementing an input validation loop.
Last week the test requirement was to prove that your solutions worked for one of the coupon tiers. This week your solution will test each of the possible coupon tiers by using a loop to iterate through all the possible test cases in one program execution.
Testing Requirements
Input Error Checking: Validate user input. Test for both non-numeric, and negative values. If the user does not supply valid input, use a loop to keep the user from advancing until a correct value (a number >= 0) is entered. This technique of using a loop to repeat prompting until correct input is entered is sometimes referred to as 'pigeonholing' the user until he/she gets it right.
Testing Requirements: Using looping control structures, run your program through an entire suite of candidate test data. Include the following test cases: apple, -1, 0, 10, 70, 160, 222. Note: Your program needs to execute all 7 test cases in one one of your program.
Here are some other requirements (remaining from lab 3's spec):
Here are some requirements specific to this expanded lab 4 spec:
4. Run a test suite that validates user input. Do not allow the user to advance until a valid data entry is provided.
5. Your submitted test run should include all the specified test cases: apple, -1, 0, 10, 70, 160, 222. All test cases are to be executed in one program run by using a loop structure.
Here is an example run:
''' Please enter the cost of your groceries: apple Enter cost >= 0: -1 Enter cost >= 0: 0 You win a discount coupon of $0.00. (0% of your purchase) Please enter the cost of your groceries: 10 You win a discount coupon of $0.80. (8% of your purchase) Please enter the cost of your groceries: 70 You win a discount coupon of $7.00. (10% of your purchase) Please enter the cost of your groceries: 160 You win a discount coupon of $19.20. (12% of your purchase) Please enter the cost of your groceries: 222 You win a discount coupon of $31.08. (14% of your purchase) '''
Tips and requirements:
# using loop to iterate several times while True: # reading and validating the cost from user cost = input('Please enter the cost of your groceries: ') if cost.isdigit() == False: cost = input('Enter cost >= 0: ') while int(cost) < 0: cost = input('Enter cost >= 0: ') elif int(cost) < 0: while int(cost) < 0: cost = input('Enter cost >= 0: ') # variable to store results cost = int(cost) percent = 0 discount_coupon = 0 # determining the discount amount and the coupon if cost < 10: percent = 0 discount_coupon = 0 elif cost < 60: percent = 8 discount_coupon = cost * percent / 100 elif cost < 150: percent = 10 discount_coupon = cost * percent / 100 elif cost < 210: percent = 12 discount_coupon = cost * percent / 100 else: percent = 14 discount_coupon = cost * percent / 100 # display the result print('You win a discount coupon of ${}. ({}% of your purchase)'.format(round(discount_coupon, 2), percent)) print() # asking user if they want to continue or not choice = input('You want to continue(y/n):') if choice != 'y': break
FOR HELP PLEASE COMMENT.
THANK YOU.