Elena
Elena

Reputation: 65

While loop with min and max conditions Python

I would like to charge a car under the condition that in the end it is charged at least 75% and if prices are positive I would like it to continue charging, but not surpass the maximum of 100%. The list of prices are the prices for everytime I charge (or decide not to charge) the car.

So here is what I have so far:

maxF = 100
minF = 75
SoC = 55
i = 0
chargeRate = 10
price = [2,-2,3,4]

while SoC < minF:
   if price[i] > 0:
       SoC += chargeRate
       i += 1
   # if there is no time left (no prices available), I have to charge in order to reach 75%
   elif (minF - SoC)/chargeRate <= (len(price) - i):
       SoC += chargeRate
       i += 1
   else:
       i += 1
print(SoC)

In this piece of code the car charges prices 2 and 3, but not 4. I don't know how to include for the car to continue charging, after having passed the 75%.

I am greatful for any suggestions.

Many thanks, Elena

Upvotes: 1

Views: 415

Answers (1)

squareRoot17
squareRoot17

Reputation: 890

From my understanding, you should try the following code:

 while SoC <= 100:
   if (i < len(price)) and (SoC < 75 or price[i] > 0):
       SoC += chargeRate
   else:
       break
   i+=1
 print(SoC)

This should work for you. I'm not sure what you are trying to do with the line

elif (minF - SoC)/chargeRate <= (len(price) - i):

Upvotes: 2

Related Questions