Learn String Manipulation by Building a Cipher - Step 26

This instruction has the opening line “At the end of your loop body”. But I asked ChatGPT where the “loop body” technically ends, and it says the end of the loop body is after the last indented statement after the colon (:). And the last indented statement from the previous step (Step 25) was “print(index)”. So, is “print(…)” not part of the loop body, even though it’s indented, and if not, why?

Could you share your code and link to the challenge in question?

Hello RSO_Media,

Assuming you haven’t changed any code yet. print(char, index) is still part of the loop body. You can see this in the preview next to the code. Every loop a char and it’s index gets printed.

As you guessed indentation is how you know what is part of something. Every time you make a loop, if statement or anything that has a body, it needs to be indented and everything that follows that indentation will stay a part of it, untill you break out of it. For example:

def my_function(): # Part of the main script
     number = 5; #Part of the function body
     
     if number == 5: # Part of the function body
          print(number) # Part of the if statement body.
          number = 6 # Part of the if statement body.

     print(number) # Part of the function body

Here’s the link:

Here’s the code I had originally:
text = ‘Hello World’
shift = 3
alphabet = ‘abcdefghijklmnopqrstuvwxyz’

for char in text.lower():
index = alphabet.find(char)
print(char, index)

And here’s my confusing conversation with ChatGPT (my prompts might have been unclear, and may be the source of the confusion):

You

where would I add a new variable at the end of a loop body?

ChatGPT

If you want to add a new variable at the end of a loop body in Python, you can do so right after the loop body by maintaining the same level of indentation as the loop. Here’s an example:

pythonCopy code

numbers = [1, 2, 3, 4, 5]
sum_numbers = 0  # Initialize a variable before the loop

for num in numbers:
    sum_numbers += num  # Accumulate the sum during each iteration

average = sum_numbers / len(numbers)  # Calculate the average after the loop

In this example, the loop iterates through each number in the list numbers, accumulating the sum in the variable sum_numbers. After the loop, a new variable average is calculated by dividing the sum by the length of the list.

Make sure to maintain proper indentation to indicate that the calculation of the average is outside the loop. The end of the loop body is determined by the decrease in indentation.

Remember, the placement of code after the loop depends on your specific requirements. If you need to perform calculations or actions based on the loop’s results, you can place that code immediately after the loop body, maintaining proper indentation.

You

I was trying to solve this question:Step 26 At the end of your loop body, declare a variable called new_index and assign the value of index + shift to this variable.

ChatGPT

Certainly! If you want to declare a variable called new_index and assign the value of index + shift at the end of your loop body, you can do it like this:

pythonCopy code

# Assuming you have the variables index and shift defined before the loop
index = 5
shift = 2

# Your loop
for item in iterable:
    # Your loop body

    # ...

    # End of loop body
    new_index = index + shift
    print(new_index)  # or do whatever you need with new_index

In this example, new_index is declared and assigned the value of index + shift at the end of each iteration of the loop. This ensures that new_index is updated at the end of each iteration based on the current values of index and shift. Adjust the loop body according to your specific context and requirements.

You

It’s kind of confusing, because the statement says “At the end of your loop body…”, but I thought the print function was part of the loop body based on your earlier description. The print function is also indented.

ChatGPT

You’re correct, and I appreciate the clarification. I misunderstood your question. If the instruction is to declare a variable called new_index and assign the value of index + shift at the end of the loop body, it would look something like this:

pythonCopy code

# Assuming you have the variables index and shift defined before the loop
index = 5
shift = 2

# Your loop
for item in iterable:
    # Your loop body

    # ...

    # The end of the loop body is here
    # Declare a variable called new_index and assign the value of index + shift
    new_index = index + shift

# Now, new_index will have the value of index + shift after the loop is completed
print(new_index)

In this corrected example, new_index is declared and assigned the value of index + shift outside the loop. This ensures that new_index will have the final value after the loop is completed. I appreciate your patience, and I hope this clarifies the concept.

You

Why do they write these instructions in a way that seems to deliberately confuse people?

All I’m going to say is in the linked step the print(char, index) is definitely part of the loop body.

This is the whole loop:

for char in text.lower():
    index = alphabet.find(char)
    print(char, index)

Notice in the preview every character from the 'Hello World' string is printed one-by-one, with the accompanying index from the alphabet string. That wouldn’t happen if print would be outside of the loop.

1 Like

I am not sure you get many advantages from chatting with ChatGPT, come to the forum for help

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.