Jump to content

Using loop counters to input files (Python3)

lewdicrous
Go to solution Solved by lewdicrous,

These worked:

From what @mariushm and @M.Yurizaki wrote:

x = 1
while x <= 5:
    y = 1
    while y <= 2:
        infile = open("Team%d_Round%d.txt" %(x,y), "r")
        score = infile.readline()
        while score != "":
            print (score, end="")
            score = infile.readline()
            print ()
        y += 1
    x += 1

And this is what @vorticalbox wrote:

for x in range(1,6):
    for y in range(1,3):
        with open("Team%d_Round%d.txt" %(x,y), "r") as f:
            for line in f.readlines():
                print(line)

 

Thank you!

 

Note: I changed them a bit so that they print the results from the first round first and then the second round (in my program), other than that it's the same.

x = 1
y = 1
while x <= 5:
    while y <= 2:
        infile = open("Team%d_Round%d.txt" %(x,y), "r")
        score = infile.readline()
        while score != "":
            print (score, end="")
            score = infile.readline()
        x += 1
        y += 1
        print ()

5abd67c7305a0_Screenshot-2018-3-30MicrosoftWord-HW3_updated_Abirdocx-HW3_SP18pdf.png.faee0eea19bd05789e1c5030a2292bb3.png

Hey, I'm trying to create a program that will read values from text files by using a loop but I have 10 files (teams) and each team has 2 files (rounds).

It currently only prints values from 2 files, I've tried to write something different to make it work but it still doesn't give me the values from all 10 files.

 

What am I doing wrong?

 

EDIT: There are 5 teams, not 10, but each team has 2 files (2 rounds), that's why there are 10 files.

Link to comment
Share on other sites

Link to post
Share on other sites

What programming language is that, python?

 

how do you determine the instructions in a while loop, by indentation or what ?  

 

As you wrote the code, Is x+= 1 and y+=1 supposed to be done in the  while (y <=2)  ?  or you would have just y +=1 in the while y loop , and x+=1 in the while x loop (reduce indentation for x

 

 


 

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, Pangea2017 said:

i am not into phyton but this does not look right.

It prints an empty line, I added it cause the last value of the first file is right on top of the first value from the second file; just wanted to add a space between them

 

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, mariushm said:

What programming language is that, python?

2 minutes ago, mariushm said:

how do you determine the instructions in a while loop, by indentation or what ?

Yes for both

2 minutes ago, mariushm said:

As you wrote the code, Is x+= 1 and y+=1 supposed to be done in the  while (y <=2)  ?  or you would have just y +=1 in the while y loop , and x+=1 in the while x loop (reduce indentation for x

I'll try that, I tried something similar but it didn't work, maybe I did it wrong

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

x = 1
y = 1
while x <= 5:
    while y <= 2:
        infile = open("Team%d_Round%d.txt" %(x,y), "r")
        score = infile.readline()
        while score != "":
            print (score, end="")
            score = infile.readline()
            print ()
    x += 1
    y += 1

@mariushmWhen i do this it gives me an infinite loop

Link to comment
Share on other sites

Link to post
Share on other sites

7 minutes ago, lewdicrous said:

It prints an empty line

Just print prints an empty line. Or if you need a new line to add to one of your earlier print statements, you can add \n or import os and add os.linesep

 

3 minutes ago, lewdicrous said:

x = 1
y = 1
while x <= 5:
    while y <= 2:
        infile = open("Team%d_Round%d.txt" %(x,y), "r")
        score = infile.readline()
        while score != "":
            print (score, end="")
            score = infile.readline()
            print ()
    x += 1
    y += 1

When i do this it gives me an infinite loop

 

The while y <= 2: exit condition is never reached, because y is modified outside of the loop

Link to comment
Share on other sites

Link to post
Share on other sites

your code needs to be something like this :


 

while x <= 6
	y = 1
	while y <= 2
	
		open file with x and y 
		while read lines 
			print lines or do math with each score
		end while read lines
		y = y +1
	end while y
	
	x = x + 1

end while x 

you don't want to increment x in the same while where you increment y

you want  x =1 , y = 1  , then x = 1 , y = 2 , then x = 2 , y =1 , then x = 2 , y = 2 etc etc

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, M.Yurizaki said:

Just print prints an empty line.

In python3 you need to add the parenthesis, maybe you didn't have to in python2, but they changed it

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, M.Yurizaki said:

It's important to say which Python version you're using, if you're using Python. Otherwise these things happen :P 

I wrote it in the tag but I'll add it to the title

Link to comment
Share on other sites

Link to post
Share on other sites

These worked:

From what @mariushm and @M.Yurizaki wrote:

x = 1
while x <= 5:
    y = 1
    while y <= 2:
        infile = open("Team%d_Round%d.txt" %(x,y), "r")
        score = infile.readline()
        while score != "":
            print (score, end="")
            score = infile.readline()
            print ()
        y += 1
    x += 1

And this is what @vorticalbox wrote:

for x in range(1,6):
    for y in range(1,3):
        with open("Team%d_Round%d.txt" %(x,y), "r") as f:
            for line in f.readlines():
                print(line)

 

Thank you!

 

Note: I changed them a bit so that they print the results from the first round first and then the second round (in my program), other than that it's the same.

Link to comment
Share on other sites

Link to post
Share on other sites

7 hours ago, lewdicrous said:

x = 1
y = 1
while x <= 5:
    while y <= 2:
        infile = open("Team%d_Round%d.txt" %(x,y), "r")
        score = infile.readline()
        while score != "":
            print (score, end="")
            score = infile.readline()
        x += 1
        y += 1
        print ()

5abd67c7305a0_Screenshot-2018-3-30MicrosoftWord-HW3_updated_Abirdocx-HW3_SP18pdf.png.faee0eea19bd05789e1c5030a2292bb3.png

Hey, I'm trying to create a program that will read values from text files by using a loop but I have 10 files (teams) and each team has 2 files (rounds).

It currently only prints values from 2 files, I've tried to write something different to make it work but it still doesn't give me the values from all 10 files.

 

What am I doing wrong?

Are you sure a while loop is the best solution to this?

 

Do you always have 10 files?

Could there be more?

 

You don't want to have to edit your program if you add more teams or need to take teams away.

 

Just things to think about.

 

Seeing as you know the amount of times you need to loop a for loop would be a better option


for x in range(5):

    for y in range(2):

        with open("Team%d_Round%d.txt" %(x,y), "r") as f:

            for line in f.readlines()

                print(line)

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, vorticalbox said:

Are you sure a while loop is the best solution to this?

I tried using for loops but I made them too complicated for myself.

3 hours ago, vorticalbox said:

Do you always have 10 files?

Could there be more?

 

You don't want to have to edit your program if you add more teams or need to take teams away.

There is a fixed number of teams, 5 teams with 2 files for each, therefore 10 files, X is for the teams and Y is for the rounds. What I wrote (10 teams) was wrong, my apologies.

3 hours ago, vorticalbox said:

Seeing as you know the amount of times you need to loop a for loop would be a better option

I'll try it out, thanks

Link to comment
Share on other sites

Link to post
Share on other sites

@vorticalbox I tried your code and it worked, but after I changed the range;

for x in range(1,6):
    for y in range(1,3):

If the you write range(n) then it will start from 0 to n - 1, so instead, I wrote range(1,n+1) just cause 0 isn't in any file name.

Thanks!

Link to comment
Share on other sites

Link to post
Share on other sites

54 minutes ago, lewdicrous said:

@vorticalbox I tried your code and it worked, but after I changed the range;


for x in range(1,6):
    for y in range(1,3):

If the you write range(n) then it will start from 0 to n - 1, so instead, I wrote range(1,n+1) just cause 0 isn't in any file name.

Thanks!

for got about that my bad glad you worked it out. 

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, lewdicrous said:

@vorticalbox I tried your code and it worked, but after I changed the range;


for x in range(1,6):
    for y in range(1,3):

If the you write range(n) then it will start from 0 to n - 1, so instead, I wrote range(1,n+1) just cause 0 isn't in any file name.

Thanks!

seeing as the files are always named the same way you could just load them all into an array and split the name then load the file. Bit more advanced and I don;t have time to show you an example just now but doing it that way would mean it won't matter how may teams or games.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

16 minutes ago, vorticalbox said:

-snip-

5abe1c2208ed0_Screenshot(11).png.954593ff9d84b6736d848191855c159c.png

It's something like this, ignore 'ResultsRound1 & 2' and 'TotalResult'

I'm only printing it right now just so I can check if the program is reading the correct thing, I have to do several calculations to those values which would then be printed into the 3 files I mentioned under the pic.

 

No need to create the array. Thank you!

Link to comment
Share on other sites

Link to post
Share on other sites

33 minutes ago, lewdicrous said:

5abe1c2208ed0_Screenshot(11).png.954593ff9d84b6736d848191855c159c.png

It's something like this, ignore 'ResultsRound1 & 2' and 'TotalResult'

I'm only printing it right now just so I can check if the program is reading the correct thing, I have to do several calculations to those values which would then be printed into the 3 files I mentioned under the pic.

 

No need to create the array. Thank you!

https://repl.it/@vorticalbox/NoxiousPresentSymbol

 

import os
import re
dir_path = os.path.dirname(os.path.realpath(__file__))

data = {}

for file in os.listdir(dir_path):
  match = re.match(r"(Team.)(Round.)", file, re.I)
  if match:
    item = match.groups()
    #if team doesn't exist
    try:
      test = data[item[0]]
    except:
      data[item[0]] = {}
      
    data[item[0]][item[1]] = [line.strip() for line in open(os.path.join(dir_path, file))]

      
    
print(data)

 

What you get in a objects of teams each having an object of the round number which is an array of all the scores

 


{  
   'Team1':{  
      'Round2':[  
         '321',
         '321'
      ],
      'Round1':[  
         '123',
         '123'
      ]
   },
   'Team2':{  
      'Round1':[  
         '456',
         '789'
      ]
   }
}

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×