Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

The following code is the program for an assignment last week. The assignment was to create the program that you would enter information into as the cashier and it would total everything up for you based on my information entered. Part of my week's assignment is to enhance it so multiple items can be handled by this program, using the while loop. This improved program should output this total for all items.

This is the example of the output each professor is looking for.

Original ticket price: $100

Is this item reduced?y

Is this item taxable?y

Here is my bill

Orginal Price $100.00

Reduced Price $25.00

Final Price $75.00

7% Sales Tax $5.25

Total amount due $80.25

Original ticket price: $75

Is this item reduced?y

Is this item taxable?n

Here is your bill

Orginal Price $75.00

Reduced Price $18.75

Final Price $56.25

7% Sales Tax $0.00

Total amount due $56.25

Total amount due is $136.50

# Enter constants for sale & salesTax

SALE = .25

SALES_TAX = .07

# Enter the ticket price of the item

origPrice = float(input('Original ticket price or 0 to quit: $'))

# Is the item reduced? Y/y or N/n - use if/elif to determine salePrice

reduced = input('Is this item reduced?')

if reduced == 'Y' or  reduced == 'y':

    salePrice = origPrice * SALE

elif reduced == 'N' or reduced == 'n':

    salePrice = 0.00

# Enter constant for finalPrice = origPrice - salePrice

finalPrice = origPrice - salePrice

# Is the item taxable? Y/y or N/n - use if/elif to determine tax

taxable = input('Is this item taxable?')

if taxable == 'Y' or taxable == 'y':

    tax = finalPrice * SALES_TAX

elif taxable == 'N' or taxable == 'n':

    tax = 0.00

# Enter all Print Statements

print('Here is your bill')

print('Orginal Price $', format(origPrice, ',.2f'),sep='')

print('Reduced Price $', format(salePrice, ',.2f'),sep='')

print('Final Price $', format(finalPrice, ',.2f'),sep='')

print('7% Sales Tax $', format(tax, ',.2f'),sep='')

print('Total amount due $', format(finalPrice + tax, ',.2f'),sep='')

1 Answer

0 votes
by (36.8k points)
edited by

It seems like you'll need to wrap the current code (excluding constant declarations) in the while True loop with a break condition, but also add another variable named totalAmountDue. This variable will be changed whenever the new item is added. If you were to apply these changes, it should look like this:

# Enter constants for sale & salesTax

SALE = .25

SALES_TAX = .07

# Counter for the total amount of money from all items, this includes tax as well

totalAmountDue = 0

while True:

    # Enter the ticket price of the item

    origPrice = float(input('Original ticket price or 0 to quit: $'))

    # Breaks out of the loop once the user wants to quit

    if (origPrice == 0):

        break

    # Is the item reduced? Y/y or N/n - use if/elif to determine salePrice

    reduced = input('Is this item reduced?')

    if reduced == 'Y' or  reduced == 'y':

        salePrice = origPrice * SALE

    elif reduced == 'N' or reduced == 'n':

        salePrice = 0.00

    # Enter constant for finalPrice = origPrice - salePrice

    finalPrice = origPrice - salePrice

    # Is the item taxable? Y/y or N/n - use if/elif to determine tax

    taxable = input('Is this item taxable?')

    if taxable == 'Y' or taxable == 'y':

        tax = finalPrice * SALES_TAX

    elif taxable == 'N' or taxable == 'n':

        tax = 0.00

    # Adds the final price of this product to the total price of all items 

    totalAmountDue += finalPrice + tax

    # Enter all Print Statements

    print("Here's the breakdown for this item: ")

    print('Orginal Price $', format(origPrice, ',.2f'),sep='')

    print('Reduced Price $', format(salePrice, ',.2f'),sep='')

    print('Final Price $', format(finalPrice, ',.2f'),sep='')

    print('7% Sales Tax $', format(tax, ',.2f'),sep='')

    print('Total amount due $', format(finalPrice + tax, ',.2f'), "\n",sep='')

print("\nTotal amount due for all items: $", format(totalAmountDue, ',.2f'))

This is the output of the edited version:

Original ticket price or 0 to quit: $10

Is this item reduced?n 

Is this item taxable?y

Here's the breakdown for this item: 

Orginal Price $10.00

Reduced Price $0.00

Final Price $10.00

7% Sales Tax $0.70

Total amount due $10.70

Original ticket price or 0 to quit: $23123123123

Is this item reduced?n

Is this item taxable?y

Here's the breakdown for this item: 

Orginal Price $23,123,123,123.00

Reduced Price $0.00

Final Price $23,123,123,123.00

7% Sales Tax $1,618,618,618.61

Total amount due $24,741,741,741.61

Original ticket price or 0 to quit: $0

Total amount due for all items: $ 24,741,741,752.31

 Want to be a master in Data Science? Enroll in this Data Science Courses

Browse Categories

...