Python Forum
Break within an if statement inside a while loop not working
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Break within an if statement inside a while loop not working
#1
Hey I am new to all this python stuff so this may be an obvious question. I am trying to process a .pdf into a list so can output it to a .pdf form. If someone could look at my code and let me know why my break statement is not working I would appreciate it!

# importing required modules
from pypdf import PdfReader

# creating a pdf reader object
reader = PdfReader('All_in_One_CCM.pdf')

# printing number of pages in pdf file
#print(len(reader.pages))
outputLst = []
pageIndx = 0
txtIdx = 0

for pages in reader.pages:
    page = reader.pages[pageIndx]
    text = page.extract_text()
    text = text.splitlines()
    outputLst += text
    pageIndx += 1

text = outputLst
while txtIdx < len(text):
    while text[txtIdx].isupper() and not text[txtIdx].startswith("CHAPTER"):
        print(text[txtIdx])
        if text[txtIdx].endswith("ORC") or text[txtIdx].endswith("CCC"):
            txtIdx += 1
            break
        else:
            txtIdx += 1

        while not text[txtIdx].startswith("_____________ did"):
            print("\t" + text[txtIdx])
            txtIdx += 1

        while not text[txtIdx].startswith("Note") or text[txtIdx].startswith("ORC") or text[txtIdx].startswith("CCC"):
            print("\t\t" + text[txtIdx])
            txtIdx += 1

    else:
        txtIdx += 1
deanhystad write Sep-02-2025, 08:40 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Attached Files

.py   TestTxtList.py (Size: 1.06 KB / Downloads: 67)
Reply
#2
What pypdf are you using? 6? What is your OS? What version python are you using?

If you have this:
while something:
    while something_else:
        break
The break statement will only break out of the something_else loop. The something loop continues to run. I don't know if that's what you are expecting. You didn't describe what's happening and how it differs from what you expect to happen.

This may not do what you think it does:
while text[txtIdx].isupper() and not text[txtIdx].startswith("CHAPTER"):
The loop ends it text[txtIdx] is not upper or text[txtIdx] starts with "CHAPTER".

I'm pretty sure this does not do what you think it does.
    while text[txtIdx].isupper() and not text[txtIdx].startswith("CHAPTER"):
        # body 1
    else:
        # body 2
This line:
while text[txtIdx].isupper() and not text[txtIdx].startswith("CHAPTER"):
and this line:
    else:
are indented the same level, so this is a while/else statement. Somewhat unusual. Python interprets this as:
while text[txtIdx] is upper and doesn't start with "CHAPTER" execute body 1. If the loop exits naturally (while condition is False), execute body 2. If the loop ends because of a break statement, do not execute body 2. You can read about while/else statements here:

https://www.geeksforgeeks.org/python/python-while-else/

Whey do you do this?
for pages in reader.pages:
    page = reader.pages[pageIndx]
    text = page.extract_text()
    text = text.splitlines()
    outputLst += text
    pageIndx += 1
This should do the same thing
for page in reader.pages:
    output_list += page.extract_text().splitlines()
In your code page and pages are the same thing. You don't need to keep track of the page index.

The indexing here is probably messing you up.
text = outputLst
while txtIdx < len(text):
    while text[txtIdx].isupper() and not text[txtIdx].startswith("CHAPTER"):
        print(text[txtIdx])
        if text[txtIdx].endswith("ORC") or text[txtIdx].endswith("CCC"):
            txtIdx += 1
            break
        else:
            txtIdx += 1
 
        while not text[txtIdx].startswith("_____________ did"):
            print("\t" + text[txtIdx])
            txtIdx += 1
 
        while not text[txtIdx].startswith("Note") or text[txtIdx].startswith("ORC") or text[txtIdx].startswith("CCC"):
            print("\t\t" + text[txtIdx])
            txtIdx += 1
 
    else:
        txtIdx += 1
This code increments the index.
        if text[txtIdx].endswith("ORC") or text[txtIdx].endswith("CCC"):
            txtIdx += 1
            break
        else:
            txtIdx += 1
So the index changed, but you don't test if the index is still in range before you do this.
        while not text[txtIdx].startswith("_____________ did")
I don't understand the logic in this program. Some example text or a description would help.
Reply
#3
Well, I think we can agree: what you have there is a mess!

No idea what you really want to do. Maybe this will give you some ideas:

text = 'Peter Piper picked a pick of pickled peppers.'
text_index = 0

for t in range(len(text)):
    if text[t].isupper():
        print(f'text[t] = {text[t]} ')
        if text[t] == 'P':
            text_index +=1
            print(f'text_index = {text_index} ')
The above gives:

Output:
text[t] = P text_index = 1 text[t] = P text_index = 2
Or this:

text_list = text.split()
word_index = 0
for word in text_list:
    if word[0] == 'p':
        word_index +=1
        print(f'word is: {word}, word_index = {word_index} ')
Result:

Output:
word is: picked, word_index = 1 word is: pick, word_index = 2 word is: pickled, word_index = 3 word is: peppers., word_index = 4
Good luck with your mess!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable being erased inside of if statement deusablutum 8 3,361 Jun-15-2024, 07:00 PM
Last Post: ndc85430
  Variable definitions inside loop / could be better? gugarciap 2 2,107 Jan-09-2024, 11:11 PM
Last Post: deanhystad
  How to create a variable only for use inside the scope of a while loop? Radical 10 14,916 Nov-07-2023, 09:49 AM
Last Post: buran
Photo Python code: While loop with if statement HAMOUDA 1 2,623 Sep-18-2023, 11:18 AM
Last Post: deanhystad
Question If, elif, and else statement not working PickleScripts 3 2,417 Mar-30-2023, 02:53 PM
Last Post: PickleScripts
  while loop not working-I am using sublime text editor mma_python 4 3,000 Feb-05-2023, 06:26 PM
Last Post: deanhystad
  Multiply and Addition in the same loop statement with logic. joelraj 2 2,458 Feb-02-2023, 04:33 AM
Last Post: deanhystad
  If statement not working correctly? MrKnd94 2 2,092 Nov-16-2022, 02:49 AM
Last Post: deanhystad
  Code won't break While loop or go back to the input? MrKnd94 2 2,806 Oct-26-2022, 10:10 AM
Last Post: Larz60+
  Help adding a loop inside a loop Extra 31 12,046 Oct-23-2022, 12:16 AM
Last Post: Extra

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020