Jan-13-2022, 08:20 PM
(This post was last modified: Jan-13-2022, 08:20 PM by deanhystad.)
You don't reset op_count. You probably don't want to reset op_code because it keeps track of the total number of "+" steps. Stepping from "a" to "i" is 8 "+" steps. Stepping "e" to "v" is 17 "+" steps. 17 + 8 == 25. Oops! Your code says there are 9 and 27 steps. Where are the extra steps coming from?
The extra step is here:
a->b->c->d->e->f->g->h->i
If you count the letters you get 9, but you should be counting the steps, not the letters. Each "->" is a step. Count them up and you get 8 steps. Your loop should go something like this:
The extra step is here:
for i in range(valS, valF-1, -1): op_count += 1You are counting fence posts when you should be counting sections. Take the case of stepping "a" to "i". The steps would be:
a->b->c->d->e->f->g->h->i
If you count the letters you get 9, but you should be counting the steps, not the letters. Each "->" is a step. Count them up and you get 8 steps. Your loop should go something like this:
for i in range(valS, valF, -1): op_count += 1This gives the correct number of steps, but why are you looping at all? You already know the number of steps = valF - valS. You don't need to write a loop to increment a counter if you already know how many times the loop will execute. Would you do math like this?
# Add 8 + 17 total = 0 for _ in range(8): total += 1 for _ in range(17): total += 1I hope not! But this is what you are doing in your code. You have too many loops in your code. There should only be one. You also have too many "if" statements. You only need one (or maybe none).