Python 'For' Loop Iterate Array
10,921
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.
Author by
Jay
Updated on June 04, 2022Comments
-
Jay almost 2 years
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
-
Joran Beasley over 5 years
price = price - (price * .1);print(day, price)
-
PM 2Ring over 5 years"I need to figure a discounted price based on the previous days' discounted price in an array". Ok, but that code just uses an array (actually, it's a list) for the day names, but it sounds like you're also supposed to use one for the discounted prices as well. As the answers below show, you don't actually need that, but maybe the point of this exercise is to create a list that contains the discounted price for each day.
-
Sheldore over 5 years@JoranBeasley: Your answer will work if either price is defined inside the function OR price is passed to the function in a variable also called price OR you will have to define global price inside the function.
-
Joran Beasley over 5 years@Bazingaa :) thanks for the clarification :)
-
PM 2Ring over 5 yearsWhy on earth was this downvoted? Jay asked a clear & simple question, and showed us his code, along with expected output and actual output.
-
Sheldore over 5 years@PM2Ring: Here's my upvote to balance
-
PM 2Ring over 5 yearsIf one of the answers below fixes your issue, you should accept it (click the check mark next to the appropriate answer). That does two things. It lets everyone know your issue has been resolved to your satisfaction, and it gives the person that helps you credit for the assist. See here for a full explanation.
-
-
T Burgis over 5 yearsIt would be better to calculate the price using
price = price * 0.9
-
Sheldore over 5 yearsGives
UnboundLocalError: local variable 'price' referenced before assignment
. If I useglobal price
inside the function and before the for loop as one of the answers below, it works -
PM 2Ring over 5 yearsYou could use
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. -
Sheldore over 5 years@nosklo: I know, it is to replace the OP's for loop. But unless I define global price inside the function, I am getting this UnboundLocalError.
-
mad_ over 5 yearsI am usually not a fan of using global but in this case, I believe the final price should be the result of iterations in the sense that it will reduce 7 times as original price and should be able to consume later.
-
Sheldore over 5 years@mad_: If I try the code without
global price
, I getUnboundLocalError: local variable 'price' referenced before assignment
-
mad_ over 5 years@Bazingaa obviously price has not been declared inside the funtion
-
Sheldore over 5 yearsYeah, so the solution where you just use
price = price - (price * .1)
without passing the price to the function would not work. That's what I am saying -
Jay over 5 yearsAll very helpful beta. Appreciate the insight.