How do I Remove the Very Last Letter in a Line of Letters

Viewed 57

I am having a bit of trouble removing one extra letter that is constantly popping up in my two lines, after my program evaluated all the numbers.

What it Should Produce:

How many steps did Nick take forwards: 4

How many steps did Nick take backwards: 2

How many steps did Bill take forwards: 5

How many steps did Bill take backwards: 3

How many steps were needed to finish the game: 13

Nick FFFFBBFFFFBBF = 5 paces from the start

Bill FFFFFBBBFFFFF = 7 paces from the start

Bill is Ahead after 13 steps

What it Does Produce:

How many steps did Nick take forwards: 4

How many steps did Nick take backwards: 2

How many steps did Bill take forwards: 5

How many steps did Bill take backwards: 3

How many steps were needed to finish the game: 13

Nick FFFFBBFFFFBBFB = 5 paces from the start

Bill FFFFFBBBFFFFFB = 7 paces from the start

Bill is Ahead after 13 steps

In the grand scheme of things, this issue isn't a huge deal, as the rest of the program works fine, but if anyone has a solution for this, I am all ears.

My Code:

#Enter how many steps Nick and Bill went forward and backward, and how many paces are needed to finish the game
A = int(input("How many steps did Nick take forwards: "))
B = int(input("How many steps did Nick take backwards: "))
C = int(input("How many steps did Bill take forwards: "))
D = int(input("How many steps did Bill take backwards: "))
S = int(input("How many steps were needed to finish the game: "))

#Counters to track how far Nick and Bill go, and how many paces they made to finish the game
counter = 0
counter2 = 0
paces = 0
paces2 = 0

#Check how many steps Nick and Bill made, and how many paces he took
print("\nNick")
while(counter < S):
  F = 0
  while(F < A):
    print("F", end="")
    F = F + 1
    paces = paces + 1
    counter = counter + 1
    if counter >= S:
      break
  b = 0
  while(b < B):
    print("B", end="")
    b = b + 1
    paces = paces - 1
    counter = counter + 1
    if counter >= S:
      break
print(" =",paces + 1 ,"paces from the start")     

#Check how many steps Bill made, and how many paces he took
print("\nBill")
while(counter2 < S):
  F2 = 0
  while(F2 < C):
    print("F", end="")
    F2 = F2 + 1
    paces2 = paces2 + 1
    counter2 = counter2 + 1
    if counter2 >= S:
      break
  b2 = 0
  while(b2 < D):
    print("B", end="")
    b2 = b2 + 1
    paces2 = paces2 - 1
    counter2 = counter2 + 1
    if counter2 >= S:
      break
print(" =",paces2 + 1 ,"paces from the start") 

#Display who won, and by how much
if paces > paces2:
  print("\nNick is Ahead after",S,"steps")
elif paces < paces2:
  print("\nBill is Ahead after",S,"steps")
elif paces == paces2:
  print("\nBill and Nick are Tied after",S,"steps")
2 Answers

In your code you use counters for the number of steps and you have a break statement after you print the values and increment the counter.

You should move it before.

You error is a very classical off-by-one error ;)

change:

  while(F < A):
    print("F", end="")
    F = F + 1
    paces = paces + 1
    counter = counter + 1
    if counter >= S:
      break

into:

  while(F < A):
    if counter >= S:
      break
    print("F", end="")
    F = F + 1
    paces = paces + 1
    counter = counter + 1

And the same in 3 other occurrences of the similar loops.

NB. I take the opportunity to tell you that while the logic of your code is good, you have a lot of repetitive code. Almost the same for both kids and for each kids almost the same for forward and backward.

What you should try to do now is to refactor your code to avoid duplicated logic. You could for example use a master loop to loop over the kids and forward/backward or better use functions to break your code into atomic tasks.

You loop while counter < S

At the end of two cycles, you have counter = 12, and the outer loop starts again.

Now, you take one step forward, realize that counter >= S, and break out of the inner while F < A loop. But you still go into the next while b < B loop before you check the value of counter.

Instead, you should check the value of the counter as a condition for both inner loops, before you take a step in the inner loop.

A, B, S = 4, 2, 13
print("\nNick")
while(counter < S):
  F = 0
  while F < A and counter < S:
    print("F", end="")
    F = F + 1
    paces = paces + 1
    counter = counter + 1
    
  b = 0
  while b < B and counter < S:
    print("B", end="")
    b = b + 1
    paces = paces - 1
    counter = counter + 1
    
print(" =",paces + 1 ,"paces from the start")

which gives:

Nick
FFFFBBFFFFBBF = 6 paces from the start
Related