Is there any way to shorten this function by way of an array, list or the like? I've tried various methods (no pun intended) but can't seem to make it work, I'm getting errors coming out the wazoo! I don't want the code, just point me in the right direction.

def main():
    print("This program finds the daily mark down value of two items on sale in our \nBoutique.")
    print()
    rate = .1
    #price = retail prices
    price = 20.00
    priceB = 25.00
    #priceW = wholesale prices
    priceW = 11.00
    priceWB = 21.00
    total = 0
    totalB = 0

    while price  >= priceW:
                total = total+1
                price = price -(price*rate)
                print("The price of the first item on day",total,"is:","$ {0:0.2f}".format(price))  
                    
    print()
    
    while priceB  >= priceWB:
                totalB = totalB+1
                priceB = priceB -(priceB*rate)
                print("The price of the second item on day",totalB,"is:","$ {0:0.2f}".format(priceB))


main()

I would define the prices in tuples and use for loop iteration over that and one while loop instead of repeating code (of course you could also put it in function). Not to give you too much here is my changed price tuple, which I used to do that to shrink the code to 17 lines (maybe three blanks).

#price = name, retail, wholesale prices tuples
    price = (('first', 20.00, 11.00), ('second', 25.00, 21.00))

I also replace priceW name with wholesale_price, which is more descriptive for me. Inside the while I used the += and -= for value updates.

Edited 5 Years Ago by pyTony: n/a

Can't you just take the difference between price and priceW, and then multiply by rate. If you want to print out every increment, then some kind of loop is necessary, and should be in a function that you pass the prices, rate, and literal to print, since both calculations are the same.

Edited 5 Years Ago by woooee: n/a

Can't you just take the difference between price and priceW, and then multiply by rate. If you want to print out every increment, then some kind of loop is necessary, and should be in a function that you pass the prices, rate, and literal to print, since both calculations are the same.

It's not a constant increment. Every loop multiplies price by (1.0-rate). The loop runs k = 1 + int( log(priceW/price)/log(1.0-rate)) times and the final price is price * (1.0 - rate)**k

Edited 5 Years Ago by Gribouillis: n/a

Thanks fellas!!! I'll have fun w/ all the above. Yes, I have to print out every increment, and it's also ness. for me to maintain some distinction between the 2 items. FYI, I've already accomodated the teachers specs, I just want something "neater" and a bit more advanced.

Thanks fellas!!! I'll have fun w/ all the above. Yes, I have to print out every increment, and it's also ness. for me to maintain some distinction between the 2 items. FYI, I've already accomodated the teachers specs, I just want something "neater" and a bit more advanced.

When there is a repetition of pattern in the code, it's always best to write a function, so I suggest something like

def display_markdown(price, pricew, rate, item_order):
    while price >= priceW: etc

then call this function twice in the main code.

Edited 5 Years Ago by Gribouillis: n/a

It's not a constant increment. Every loop multiplies price by (1.0-rate). The loop runs k = 1 + int( log(priceW/price)/log(1.0-rate)) times and the final price is price * (1.0 - rate)**k

What's log? I'm an actionscript dev, so my math is, metzah/metz, to s$&*y!!!!

When there is a repetition of pattern in the code, it's always best to write a function, so I suggest something like

def display_markdown(price, pricew, rate, item_order):
    while price >= priceW: etc

then call this function twice in the main code.

yeah, that makes sense, and if I was doing a flash project, I'd do that, but I'm in school, and caught between trying to learn the language, fundementally from the ground up, (although they are pretty similiar As3 and python) and also not wanting to look like I'm going too far ahead of the class, as the teach still has us using only one function at a time. Thanks though man, I really appreciate your help.

What's log? I'm an actionscript dev, so my math is, metzah/metz, to s$&*y!!!!

math.log() is the logarithm function. Its main property is that log(x*y) == log(x) + log(y), so that if p, r are price and rate, log(p*(1-r)**k) == log(p) + k*log(1-r) . The inverse of this function is the exponential math.exp().

Edited 5 Years Ago by Gribouillis: n/a

Attachments log.png 18.68 KB

math.log() is the logarithm function. Its main property is that log(x*y) == log(x) + log(y), so that if p, r are price and rate, log(p*(1-r)**k) == log(p) + k*log(1-r) . The inverse of this function is the exponential math.exp().

that's what I thought. I tried importing the math lib., but it still kept throwing "log, undefined errors". I'll play around w/ it when I get a chance. I spent some time trying to condense the script last night, and found that I can refine things a bit, but once I implemented the above suggestions, although more advanced, I was losing some of the "distinction" between the two items, as i ultimatley need the output to look the way I have it now..... not nearly schooled enough at PY, to get the same result swimming in your end of the pool so to speak. PRACTICE MAKES...so I'll keep practicing daily. Thank you sir!

Good luck with your program. There should be no problem computing math.log(x) as long as x > 0.0. If there are problems, you should probably reinstall python because your python is corrupted.

You do need to either import math and use the function as math.log or do from math import log and use it in module as log .

Edited 5 Years Ago by pyTony: n/a

This article has been dead for over six months. Start a new discussion instead.