Home how to display unaccumulated variables in python?
Reply: 1

how to display unaccumulated variables in python?

Dylan Sen
1#
Dylan Sen Published in 2018-02-12 19:19:50Z

I'm doing an assignment that requires me to display a menu, then prompt the customer to pick a rose type and an amount of chosen rose. I have an accumulating variable to accept and be used for the final transaction calculations. But how do I get it to display the unaccumulated/inputted variable and price whenever a customer selects to order more roses?

elif roseTypeOrdered == 'r' or roseTypeOrdered == 'R':
        numRedRosesOrdered += int(input("How many red roses would you like to order?"))
        totalPrice += numRedRosesOrdered * STEM_RED_ROSE_PRICE
        print("You have selected {amount:.0f} red roses for a price of ${price:.2f}" .format(amount=numRedRosesOrdered, price=totalPrice))
        # Make sure you have a check to see if the input only Y or N
        transactionActive = input("Would you like to order more roses? (Y-yes, N-no)")
        if transactionActive == 'N' or transactionActive == 'n':
            break

As is, if I input R, 4, Y (You have selected 4 red roses for $8.00) then R, 4, N (You have selected 8 red roses for $24.00). Red roses are $2.00 each.

Pat Jones
2#
Pat Jones Reply to 2018-02-12 19:33:37Z

This happens because each time through the loop you are adding the running total of roses numRedRosesOrdered to totalPrice, which itself is already a running total. Change numRedRosesOrdered to:

numRedRosesOrdered = int(input("How many red roses would you like to order?"))

...and then have a separate variable, say totalNumRedRoses that you define like:

totalNumRedRoses += numRedRosesOrdered

This is what you'll use in your summary print:

print("You have selected {amount:.0f} red roses for a price of ${price:.2f}" .format(amount = totalNumRedRoses , price = totalPrice))
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.31066 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO