I am new to python and I am looking for some help with loop structures, specifically how to use a 'For' loop or alternative loop to solve a problem. I need to figure a discounted price based on the previous days' discounted price in an array so it will iterate through the array an apply the 10% discount. Here is what I have so far:
opnDays = ["mon", "tue", "wed", "thr", "fri"]
price = 10
def discount(array):
for day in array:
disPrice = price - (price * .1)
print(day, disPrice)
discount(opnDays)
>>>
mon 9.0
tue 9.0
wed 9.0
thr 9.0
fri 9.0
I want to get:
>>>
mon 9.0
tue 8.1
wed 7.29
thr 6.561
fri 5.9109
Any help with how to organize this loop to get a the 10% discount from the previous day would be helpful. Thank you
5 Answers 5
Your code, as written, is close:
opnDays = ["mon", "tue", "wed", "thr", "fri"]
price = 10
def discount(array):
disPrice = price
for day in array:
disPrice *= 0.9
print(day, disPrice)
What I did here was change how disPrice was set on your loop. You were setting it to the same value (0.9 * price) on every iteration. All I did was set it to price in the beginning and multiply it by 0.9 every iteration, resulting in the desired behavior.
1 Comment
You're not changing the value of price so the same result is calculated on every iteration of the loop.
This should work:
for day in array:
price = price - (price * .1)
print(day, price)
3 Comments
price = price * 0.9UnboundLocalError: local variable 'price' referenced before assignment. If I use global price inside the function and before the for loop as one of the answers below, it worksIf you want to stick to using a function as in your code, following is one way:
opnDays = ["mon", "tue", "wed", "thr", "fri"]
price = 10
def discount(array):
disPrice = price # assign initial price to discount
for day in array:
disPrice = disPrice*0.9 # Reduce the discount by 10 percent every time
print(day, disPrice)
discount(opnDays)
Output
mon 9.0
tue 8.1
wed 7.29
thr 6.561
fri 5.9049000000000005
Comments
Your code is pretty close, but on each day you need to modify the price of the previous day.
I would pass the starting price into the function, just like you pass the list of day names. I've also used .format, so we get the prices printed as dollars & cents. This only affects how the price is printed, it doesn't affect the actual value, so we don't lose any accuracy.
I've also changed your names slightly so that they conform with the usual Python PEP-0008 style.
def discount(array, price):
for day in array:
price = price - (price * .1)
print("{}: {:.2f}".format(day, price))
open_days = ["mon", "tue", "wed", "thr", "fri"]
start_price = 10.00
discount(open_days, start_price)
output
mon: 9.00
tue: 8.10
wed: 7.29
thr: 6.56
fri: 5.90
Comments
You can use global and keep your logic as it is
opnDays = ["mon", "tue", "wed", "thr", "fri"]
price = 10
def discount(array):
global price
for day in array:
price = price - (price * .1)
print(day, price)
discount(opnDays)
In order to avoid global pass the price as an argument and the preferred approach
opnDays = ["mon", "tue", "wed", "thr", "fri"]
price = 10
def discount(array,price):
for day in array:
price = price - (price * .1)
print(day, round(price,2))
discount(opnDays,price)
In case you need the final price after successive iterations which I believe might be a requirement for you
opnDays = ["mon", "tue", "wed", "thr", "fri"]
price = 10
def discount(array,price):
for day in array:
price = price - (price * .1)
print(day, round(price,2))
return price
final_price = discount(opnDays,price)
print(final_price)
5 Comments
global for this, but it shouldn't be encouraged. Modifiable globals break program modularity. It doesn't matter much for a tiny program like this, but it's a bad habit to get into.global price, I get UnboundLocalError: local variable 'price' referenced before assignmentprice = price - (price * .1) without passing the price to the function would not work. That's what I am saying
price = price - (price * .1);print(day, price)