how can i transform this into a while loop program?

Welcome to StackOverflow! From the sample output that your professor gave you, it seems like you’ll need to wrap your current code (excluding constant declarations) in a while True loop with the break condition, but also add another variable called totalAmountDue. This variable will be changed every time a new item is added. If you were to apply the 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

If you’d like to learn more about while loops in python, you can check out this link: https://www.w3schools.com/python/python_while_loops.asp

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top