Reputation: 33
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
Upvotes: 2
Views: 18723
Reputation: 8273
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)
Upvotes: 0
Reputation: 55469
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
Upvotes: 0
Reputation: 39042
If 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
Upvotes: 0
Reputation: 7970
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.
Upvotes: 4
Reputation: 222822
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)
Upvotes: 0