1

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

2
  • 1
    your while loop will stop if SoC < minF, as you specified. what do you intend to do with the variable prices? Commented Nov 24, 2019 at 11:15
  • 2
    Not part of your question, but as you have i += 1 in every possible condition of your if statement, you should simply have it once outside of the if Commented Nov 24, 2019 at 11:15

1 Answer 1

2

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):

Sign up to request clarification or add additional context in comments.

4 Comments

I get a "list index out of range" error from your code.
@Elena my bad. I've updated the answer and this should work.
It works! Thank you so much! I just added while SoC + chargeRate <= 100 so it does not pass 100 by charging in that iteration.
Happy to help! Please accept the answer so the question can be closed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.