0

I'm encountering a problem where I need to use for a in range (n) loop to solve things out. But there's a condition where sometimes I might need to call out index[a+1] to satisfy if-conditions. I realize this would result in index out of range error, is there any solution to this or should I implement another method ? Some codes attached.

for c in range (n):
  out=enter+width[c]
  if power==cap:
         power-=(width[c])*(height[c])*2+2+enter
  elif power<cap:
         power-=(width[c])*(height[c])*2
  print("Field {:d}: completed. Battery: {:d}.".format(c+1,power))
  if (power-2-out<(cap*0.5)) or (power-(width[c+1])*(height[c+1])-2-out- 
  width[c+1]<(cap*0.5)):
         power=cap
         print("Charging...")
  enter+=width[c]
3
  • 1
    What is your desired functionality when c==n? Your current code won't necessarily lead to any errors - it depends on the number of items in the width and height lists
    – yuvgin
    Apr 3, 2019 at 17:30
  • 2
    for c in range(n-1)?
    – Rocky Li
    Apr 3, 2019 at 17:30
  • When possible, reframe your problem so that you are considering the previous element instead of the next one. You won't even need the index in that case; you just remember the last value you saw. Not sure this is possible here, though.
    – kindall
    Apr 3, 2019 at 17:36

4 Answers 4

1

In this scenario I'd say you don't need to go all the way to n.

Maybe you should change your for-loop to for c in range(n-1):

0

You could use a try/except block:

for c in range (n):
  out=enter+width[c]
  if power==cap:
    power-=(width[c])*(height[c])*2+2+enter
  elif power<cap:
     power-=(width[c])*(height[c])*2
  print("Field {:d}: completed. Battery: {:d}.".format(c+1,power))
  try:
    if (power-2-out<(cap*0.5)) or (power-(width[c+1])*(height[c+1])-2-out-width[c+1]<(cap*0.5)):
      power=cap
      print("Charging...")
  except IndexError:
    print("error message here")
  enter+=width[c]

However, you probably want to figure out a different method.

2
  • 1
    Thanks, I did look into the try/except method, but I didn't realize except IndexError could be done. Thanks again. Apr 3, 2019 at 17:42
  • @EdwardLeung if this solved your problem please check the grey/green checkmark to the left of this answer so that everyone knows it's been solved. Apr 3, 2019 at 17:45
0

If you know you are going to want to iterate through the whole range, then you can use a list comprehension to generate the range as a list, then iterate over that list:

n = 1234  # Some number
nums = [x for x in range(n)]  # List comprehension generates a full list for the given range

for i, c in enumerate(nums):
    nums[i + 1]  # Access is valid, as long as index is within bounds of list size
0

I recommend putting the condition in your code, only run the if command if it's not out of range.

for c in range(n):
  out=enter+width[c]
  if power==cap:
         power-=(width[c])*(height[c])*2+2+enter
  elif power<cap:
         power-=(width[c])*(height[c])*2
  try:
       print("Field {:d}: completed. Battery: {:d}.".format(c+1,power))
  except:
        print("out of range")
  if (power-2-out<(cap*0.5)) :
         power=cap
         print("Charging...")

  elif c + 1 <= len(n) - 1:
    if (power-(width[c+1])*(height[c+1])-2-out-width[c+1]<(cap*0.5)):
               power=cap
               print("Charging...")
  enter+=width[c]

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.