[Python] Why does this loop execute 4 times?

In summary, the code reads in an input file and extracts a grid size from lines that may contain arbitrary characters. The data extraction is functioning, but the loop executes 4 times and then moves on to the next function. The user is wondering why the loop is executing 4 times.
  • #1
Hercuflea
596
49
I've written a code which reads in an input file and extracts a number which represents a grid size, from lines in the file which can contain arbitrary characters. The extraction of the data seems to be working, but for some reason the loop executes 4 times and then moves on to the next function in my code...Why does my loop execute 4 times? I've been stuck on this for a while now.

Python:
def BOUTinp_search():
    with open('BOUT.inp', 'r') as boutinput:
        for line in boutinput:
            if '[mesh]' in line:
                print '\nFound mesh options.'
                continue
            elif 'nx =' in line:
                xnumpts = int(filter(str.isdigit, line))
                print('Recovered number of x gr'
                'id points.')
                print 'x grid size: ', xnumpts
   
                continue
            elif 'ny =' in line:
                ynumpts = int(filter(str.isdigit, line))
                print('Recovered number of y gr'
                'id points.')
                print 'y grid size: ', ynumpts
                continue
            elif 'dx =' in line:
                dx = float(filter(str.isdigit, line))
                print 'Recovered x spacing.'
                print 'x grid spacing: ', dx
                continue

            elif 'dy =' in line:
                dy = float(filter(str.isdigit, line))
                print 'Recovered y spacing.'
                print 'y grid spacing: ', dy
                continue

    return [xnumpts, ynumpts, dx, dy]

Output:

Code:
[me@dir]$ python file.py

Found mesh options.
Recovered number of x grid points.
x grid size:  260
Recovered number of y grid points.
y grid size:  1
Recovered x spacing.
x grid spacing:  2.0
Recovered y spacing.
y grid spacing:  1.0

Found mesh options.
Recovered number of x grid points.
x grid size:  260
Recovered number of y grid points.
y grid size:  1
Recovered x spacing.
x grid spacing:  2.0
Recovered y spacing.
y grid spacing:  1.0

Found mesh options.
Recovered number of x grid points.
x grid size:  260
Recovered number of y grid points.
y grid size:  1
Recovered x spacing.
x grid spacing:  2.0
Recovered y spacing.
y grid spacing:  1.0

Found mesh options.
Recovered number of x grid points.
x grid size:  260
Recovered number of y grid points.
y grid size:  1
Recovered x spacing.
x grid spacing:  2.0
Recovered y spacing.
y grid spacing:  1.0
 
Last edited by a moderator:
Technology news on Phys.org
  • #2
Hercuflea said:
I've written a code which reads in an input file and extracts a number which represents a grid size, from lines in the file which can contain arbitrary characters. The extraction of the data seems to be working, but for some reason the loop executes 4 times and then moves on to the next function in my code...Why does my loop execute 4 times? I've been stuck on this for a while now.

Python:
def BOUTinp_search():
    with open('BOUT.inp', 'r') as boutinput:
        for line in boutinput:
            if '[mesh]' in line:
                print '\nFound mesh options.'
                continue
            elif 'nx =' in line:
                xnumpts = int(filter(str.isdigit, line))
                print('Recovered number of x gr'
                'id points.')
                print 'x grid size: ', xnumpts
  
                continue
            elif 'ny =' in line:
                ynumpts = int(filter(str.isdigit, line))
                print('Recovered number of y gr'
                'id points.')
                print 'y grid size: ', ynumpts
                continue
            elif 'dx =' in line:
                dx = float(filter(str.isdigit, line))
                print 'Recovered x spacing.'
                print 'x grid spacing: ', dx
                continue

            elif 'dy =' in line:
                dy = float(filter(str.isdigit, line))
                print 'Recovered y spacing.'
                print 'y grid spacing: ', dy
                continue

    return [xnumpts, ynumpts, dx, dy]

Output:

Code:
[me@dir]$ python file.py

Found mesh options.
Recovered number of x grid points.
x grid size:  260
Recovered number of y grid points.
y grid size:  1
Recovered x spacing.
x grid spacing:  2.0
Recovered y spacing.
y grid spacing:  1.0

Found mesh options.
Recovered number of x grid points.
x grid size:  260
Recovered number of y grid points.
y grid size:  1
Recovered x spacing.
x grid spacing:  2.0
Recovered y spacing.
y grid spacing:  1.0

Found mesh options.
Recovered number of x grid points.
x grid size:  260
Recovered number of y grid points.
y grid size:  1
Recovered x spacing.
x grid spacing:  2.0
Recovered y spacing.
y grid spacing:  1.0

Found mesh options.
Recovered number of x grid points.
x grid size:  260
Recovered number of y grid points.
y grid size:  1
Recovered x spacing.
x grid spacing:  2.0
Recovered y spacing.
y grid spacing:  1.0
How many lines are there in your input file? You have a loop that executes for each line in this file. Also, I don't see the reason for having continue statements in each of your if clauses.

BTW, I removed your "spoiler" tags. Since you're asking a question about code, having your code and output hidden by spoiler tags didn't seem very useful.
 
  • #3
Mark44 said:
How many lines are there in your input file? You have a loop that executes for each line in this file. Also, I don't see the reason for having continue statements in each of your if clauses.

BTW, I removed your "spoiler" tags. Since you're asking a question about code, having your code and output hidden by spoiler tags didn't seem very useful.
Hi Mark, thanks for the input.

There are currently 48 lines in the input file, but there could be more or less based on what the user's preferences are. At the moment I'm only interested in making a Cartesian grid using one of the options called "mesh" in the input file, but I'm trying to keep it as forward-compatible as possible.
I.e. I want to loop through the entire file in case I decide to read in options from some of the other sections later.

The continue statements were basically a safeguard to make sure it only executes one and only one of the if statements at each line of the file, but I suppose they aren't necessary.
 
  • #4
Well, maybe the contents of BOUTinp_search() is being executed only once; but, could it be that you are calling BOUTinp_search() four times in the first place? Can't tell, you don't show your entire code.
 
  • #5
gsal said:
Well, maybe the contents of BOUTinp_search() is being executed only once; but, could it be that you are calling BOUTinp_search() four times in the first place? Can't tell, you don't show your entire code.

You were right...I was actually calling the function four times in a silly way in another function. Thanks for that.
 
  • #6
Assuming your section names (like `[mesh]`) are unique, there is a component in the standard library to read files in this format. It's quicker and more robust to use it, if your file structure allows it.

https://docs.python.org/3/library/configparser.html
 

Related to [Python] Why does this loop execute 4 times?

1. Why does this loop execute 4 times?

The loop executes 4 times because it is programmed to do so. The number of times a loop executes is determined by the condition set in the loop statement.

2. How do I change the number of times the loop executes?

You can change the number of times the loop executes by modifying the condition in the loop statement. This can be done by using different comparison operators, such as less than or greater than, or by changing the value of the variable used in the condition.

3. Can I make the loop execute an infinite number of times?

Yes, you can make the loop execute an infinite number of times by using a while loop and setting the condition to always be true. However, this is not recommended as it can cause your program to crash or become stuck in an infinite loop.

4. How can I make the loop execute a specific number of times?

You can make the loop execute a specific number of times by using a for loop and specifying the number of iterations in the range of the loop statement. For example, for i in range(5): will execute the loop 5 times.

5. Why is my loop not executing at all?

There could be several reasons why your loop is not executing, such as incorrect syntax, a logical error, or the loop condition is never met. Double check your code and make sure the loop statement is properly written and that the condition is being met.

Similar threads

  • Programming and Computer Science
Replies
1
Views
10K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
17
Views
2K
Back
Top