0

A bar shop has a ten-day promotion. During this period, the price of an beer drops 10 percent each day. For example, a beer that costs 10$ on the first day costs $9 on the second day and $8.1 on the third day.

I want to write a python function that usesyield keyword to calculate beer price each day.

For instance if we give input 10, My expected output is:

Price is discounted to : 9Price is discounted to : 8.1

..etc

class DiscountCalc:    def get_item_price(self):        return input('Input a starting price (0 to quit): ')    def discount(self, current_price, days):        yield (current_price - (current_price*10)//100)    def run(self):        initial = self.get_item_price()        for price in self.discount(initial, 10):            print "Price is discounted to : " + str(price)DiscountCalc().run()
internet_user's user avatar
internet_user
3,3091 gold badge22 silver badges30 bronze badges
askedDec 6, 2017 at 19:12
Alice's user avatar
6
  • 2
    So, whatexactly is your question? As an side, this class seems pretty... unnecessary. There isn't any internal state.CommentedDec 6, 2017 at 19:15
  • If I add the "yield (current_price - (current_price*10)//100)" to my discount class, I am getting input as only "Price is discounted to : 9" which is onlay first day, but I want to get result for other days too.CommentedDec 6, 2017 at 19:18
  • Pleaseedit your question with the code you are using that is not working. I cannot tell whatexactly you mean by "If I add the "yield (current_price - (current_price*10)//100)" to my discount class".Add it where?CommentedDec 6, 2017 at 19:20
  • 1
    @Alice Please put your attempt with theyield statement in the question with the description of what it's doing and what you expect it to do.CommentedDec 6, 2017 at 19:20
  • I did, as you see I add my yield statement to my discount class, it is only giving output as only for one day, I want to get output for other days too, Probably I will use loop but, how?CommentedDec 6, 2017 at 19:23

1 Answer1

2

it's due to the cycle, this should fix it:

def discount(self, current_price, days):    for day in range(days):        yield current_price        current_price = current_price * 0.9def run(self):    initial = self.get_item_price()    for price in self.discount(initial, 10):        print(price)
answeredDec 6, 2017 at 19:29
Shailyn Ortiz's user avatar
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.