Question

In: Computer Science

IN PYTHON Your task this week is to use loops to both validate user input as...

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):

  1. Create constants versus using literals in your source code.
  2. Use a float to represent both the shopper’s grocery bill as well as the coupon amount.
  3. Format the discount coupon amount to 2 decimal places (Example: $19.20).

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:

  1. Create constants versus using literals in your source code.
  2. Use a float to represent both the shopper’s grocery bill as well as the coupon amount.
  3. Format the discount coupon amount to 2 decimal places (Example: $19.20).
  4. Show your program run for all of the test cases: -1, apple, 0, 10, 70, 160, 222. Note: Your program needs to work for all 7 test cases. Your program test run only needs to demonstrate all of the test cases in one program run.

Solutions

Expert Solution

# 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.


Related Solutions

PYTHON Write a python program that encrypts and decrypts the user input. Note – Your input...
PYTHON Write a python program that encrypts and decrypts the user input. Note – Your input should be only lowercase characters with no spaces. Your program should have a secret distance given by the user that will be used for encryption/decryption. Each character of the user’s input should be offset by the distance value given by the user For Encryption Process: Take the string and reverse the string. Encrypt the reverse string with each character replaced with distance value (x)...
Prompt the user to input their lucky number. Upon getting the users input, validate that the...
Prompt the user to input their lucky number. Upon getting the users input, validate that the input is a valid number using Int32.TryParse (more info can be found at https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/types/how-to-convert-a-string-to-a-number (Links to an external site.)). If the input is not a valid number, output a message to the user stating that you are sorry but you are unable to run the FizzBuzz process due to the input not being a valid number. If the input is valid, you are clear...
CODE IN PYTHON: Your task is to write a simple program that would allow a user...
CODE IN PYTHON: Your task is to write a simple program that would allow a user to compute the cost of a road trip with a car. User will enter the total distance to be traveled in miles along with the miles per gallon (MPG) information of the car he drives and the per gallon cost of gas. Using these 3 pieces of information you can compute the gas cost of the trip. User will also enter the number of...
(CODE IN PYTHON) Program Input: Your program will display a welcome message to the user and...
(CODE IN PYTHON) Program Input: Your program will display a welcome message to the user and a menu of options for the user to choose from. Welcome to the Email Analyzer program. Please choose from the following options: Upload text data Find by Receiver Download statistics Exit the program Program Options Option 1: Upload Text Data If the user chooses this option, the program will Prompt the user for the file that contains the data. Read in the records in...
Python #Use an input statement to ask the user for his/her age (no prompt) and assign...
Python #Use an input statement to ask the user for his/her age (no prompt) and assign the user entry to a variable #Convert the user input to an integer and assign to a variable #Write if-elif-else statements with the following conditions and actions #Important: For each Action below, use the same variable name for the rating in all the if, elif, and else statements #Condition: user input less than or equal to 9 Action: assign the letter G as a...
Use Python : Problem Set 01: The network for this task has two input nodes, one...
Use Python : Problem Set 01: The network for this task has two input nodes, one hidden layer consisting of two nodes, and one output node. It uses a ReLU activation function. For the hidden layer, the weights for the first hidden node (from the input nodes) are (2.3, -0.64, 2). The last number is the weight for the bias term. The weights for the second hidden node are (-3, -2, -1). For the output layer, the weights are (5,...
Write a program In python of Jupiter notebook (using loops) that prompts the user to enter...
Write a program In python of Jupiter notebook (using loops) that prompts the user to enter his/her favorite English saying, then counts the number of vowels in that (note that the user may type the saying using any combination of upper or lower case letters). Example: Enter your favorite English saying: Actions speak LOUDER than words. Number of wovels: 10
Python Write a program that loops, prompting the user for their full name, their exam result...
Python Write a program that loops, prompting the user for their full name, their exam result (an integer between 1 and 100), and then writes that data out to file called ‘customers.txt’. The program should check inputs for validity according to the following rules: First and last names must use only alphabetical characters. No spaces, hyphens or special characters. Names must be less than 20 characters long. Exam result (an integer between 1 and 100 inclusive) The file should record...
Write a python program that loops, prompting the user for their full name, their exam result...
Write a python program that loops, prompting the user for their full name, their exam result (an integer between 1 and 100), and then writes that data out to file called ‘customers.txt’. The program should check inputs for validity according to the following rules: First and last names must use only alphabetical characters. No spaces, hyphens or special characters. Names must be less than 20 characters long. Exam result (an integer between 1 and 100 inclusive) The file should record...
This task exercises your ability to use python to represent data and use flow control and...
This task exercises your ability to use python to represent data and use flow control and functions to re-organize the data. You need to submit the ipynb file to Moodle. A data scientist has collected tube information and saved the video info in multiple CSV files. Each CSV file has the following columns: ·         video_id ·         trending_date ·         title ·         channel_title ·         category_id ·         publish_time ·         tags ·         views    ·         likes      ·         dislikes ·         comment_count ·         thumbnail_link ·         comments_disabled     ·         ratings_disabled          ·         video_error_or_removed        ·         description You are asked to write python code to process CSV data...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT