Re: [Tutor] new to python
On 7/23/2017 1:03 AM, Alan Gauld via Tutor wrote: On 23/07/17 07:26, N6Ghost wrote: f = open("C:\coderoot\python3\level1\inputfile.txt", 'r') for line in file: Note that you have no variable called 'file'. So this line doesn't make sense. for line in f: print(line.rstripe()) This bit will work if you omit the line above and fix the indentation. (and remove the 'e' from strip() f.close() This should be outside the loop, you don't want to close the file after every line. Finally, there is another way to do this which is considered 'better'/more Pythonic: with open("C:\coderoot\python3\level1\inputfile.txt", 'r') as f: for line in f: print(line.strip()) Notice with this construct the closing of the file is handled for you. any idea why that does not work? When posting questions always include the full error text. Although apparently cryptic it actually contains a lot of useful detail which saves us from making guesses. this code works f = open("C:/coderoot/python3/level1/inputfile.txt", 'r') for line in f: for line in f: #print(line.rstrip()) print(line) f.close()f = open("C:/coderoot/python3/level1/inputfile.txt", 'r') for line in f: for line in f: #print(line.rstrip()) print(line) f.close() the out put skips the first line of the inputfile and puts a blank line inbetween inputfile is: tom jerry make windows linux -N6Ghost ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Study Python was: Re: Tutor Digest, Vol 161, Issue 36
On 24/07/17 18:41, Borisco Bizaro wrote: > Please what is the best way to study python programming well. One important skill is to read and follow instructions. For example... > On Jul 24, 2017 17:00, wrote: > >> Send Tutor mailing list submissions to >> tutor@python.org >> ... >> >> When replying, please edit your Subject line so it is more specific >> than "Re: Contents of Tutor digest..." Also, do not resend the entire digest - we have already seen the messages and some members pay by the byte for internet access. As for learning Python there are videos on Youtube, multiple online tutorials at every level and literally dozens of books. It all depends on your personal learning style and your previous programming experience. I'd suggest you start with a few YouTube videos to get a feel for things then dive into an online tutorial. The important thing is to write code. Not just what the tutorials show you but take that as a starter and modify it. See if the changes do what you expected, if not figure out why not. If you get stuck as questions here. Always tell us your Python version and OS, post the problem code (cut n paste) and the full error message if any. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] new to python
The problem here is that you have doubled the "for line in f:" line. Given that you say you know some programming, I'll just cut to the technical name of the problem you are having: You are changing the value of a loop variable (by starting an inner loop with the same loop variable) inside a loop. Doing that in Python leads to behaviour that is hard to understand and almost never what you intended. On Tue, Jul 25, 2017 at 5:58 AM, N6Ghost wrote: > > > On 7/23/2017 1:03 AM, Alan Gauld via Tutor wrote: >> >> On 23/07/17 07:26, N6Ghost wrote: >> >>> f = open("C:\coderoot\python3\level1\inputfile.txt", 'r') >>> for line in file: >> >> Note that you have no variable called 'file'. >> So this line doesn't make sense. >> >>> for line in f: >>> print(line.rstripe()) >> >> This bit will work if you omit the line above and >> fix the indentation. (and remove the 'e' from strip() >> >>> f.close() >> >> This should be outside the loop, you don't want >> to close the file after every line. >> >> Finally, there is another way to do this which >> is considered 'better'/more Pythonic: >> >> with open("C:\coderoot\python3\level1\inputfile.txt", 'r') as f: >> for line in f: >> print(line.strip()) >> >> Notice with this construct the closing of the file is >> handled for you. >> >>> any idea why that does not work? >> >> When posting questions always include the full error text. >> Although apparently cryptic it actually contains a lot of >> useful detail which saves us from making guesses. >> > > > this code works > f = open("C:/coderoot/python3/level1/inputfile.txt", 'r') > for line in f: > for line in f: > #print(line.rstrip()) > print(line) > > f.close()f = open("C:/coderoot/python3/level1/inputfile.txt", 'r') > for line in f: > for line in f: > #print(line.rstrip()) > print(line) > > f.close() > > the out put skips the first line of the inputfile and puts a blank line > inbetween > > inputfile is: > tom > jerry > make > windows > linux > > -N6Ghost > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor -- André Engels, andreeng...@gmail.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] new to python
On 25/07/17 04:58, N6Ghost wrote: > this code works > f = open("C:/coderoot/python3/level1/inputfile.txt", 'r') > for line in f: > for line in f: > #print(line.rstrip()) > print(line) > > f.close() > the out put skips the first line of the inputfile and puts a blank line > inbetween I'm not sure why you have two for loops? Why did you do that? Can you explain your thinking there? Remove one of the for... lines. Your code does this: > f = open("C:/coderoot/python3/level1/inputfile.txt", 'r') open the file and assign it to 'f' > for line in f: get the first line from f and assign it to 'line' > for line in f: print(line) get the next line from f and assign it to 'line' This overwrites the value from the first for loop above. The line is then printed. The second loop then repeats for all of the remaining lines in the file. At the end of the second for loop control returns to the top for loop. But, since the file is now empty, the top loop never gets any more values from f, so it terminates. The blank lines are caused by the fact that the lines in the file end in a newline character and print() adds a newline of its own. Either reinstate your rstrip() call or stop print() adding a newline with print(line, end='') I'm also not sure why you posted two copies of your code? I assume you only use one since otherwise you would have told us that you got two lots of output? HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor