Python Forum
Exercise 20, Learn Python 3 the Hard Way
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Exercise 20, Learn Python 3 the Hard Way
#1
This code doesn't work as Zed Shaw says it should in his book. After the rewind the first call to print does it's job, sort of, (random characters before the first line of text, and spacing seems strange) but then the only thing that prints on lines two and three are the line numbers...no text follows. Here's the code:
from sys import argv

script, input_file = argv

def print_all(f):
    print (f.read())

def rewind(f):
    f.seek(0)

def print_a_line(line_count, f):
    print (line_count, f.readline())

current_file = open(input_file)

print ("First let's print the whole file:\n")

print_all (current_file)

print ("Now let's rewind, kind of like a tape.")

rewind (current_file)

print ("Let's print three lines:")

current_line = 1
print_a_line (current_line, current_file)

current_line = current_line + 1
print_a_line (current_line, current_file)

current_line = current_line +1
print_a_line(current_line, current_file)
Reply
#2
I worked it out, though not sure it's very elegant. Put a "rewind current_file" between lines 27 - 29 and 30 - 32. Apparently, the file got read in line 27 and needed to be "rewound" before it could be read again in the following print command lines. Don't know how Zed Shaw got it to work the way it's written in his book.

Well, not really. Still have problems. I went back to the text file and put in three lines with numbers 1, 2, 3. The result is that when the script asks "First let's print the whole file.\n" I get the whole file OK. But when it drops down to the last print commands, after "Now let's print three lines" the first line of the text file prints three times. Still wondering how Zed did this. And I'm really enjoying talking to myself here. :-)
Reply
#3
(Feb-25-2018, 08:55 PM)Dixon Wrote: Don't know how Zed Shaw got it to work the way it's written in his book.
Zed Shaw is not popular here he is a clown,so we do not support his contents at all.
Our list of free resources.
Reply
#4
Worked for me as written in the book. Smile
Reply
#5
(Feb-25-2018, 10:11 PM)hersh Wrote: Worked for me as written in the book. Smile

Thanks, that's helpful.

Did you bother to look at the code I posted? I've typed it in to Atom twice and checked for typographical errors; nothing.

And typed it in to Visual Studio with the same result.
Reply
#6
(Feb-25-2018, 09:49 PM)snippsat Wrote:
(Feb-25-2018, 08:55 PM)Dixon Wrote: Don't know how Zed Shaw got it to work the way it's written in his book.
Zed Shaw is not popular here he is a clown,so we do not support his contents at all.
Our list of free resources.
Ok, you don't like Zed Shaw. But does it follow that you wouldn't help a forum member because of that?
Reply
#7
(Feb-26-2018, 04:38 PM)Dixon Wrote: Ok, you don't like Zed Shaw. But does it follow that you wouldn't help a forum member because of that?
No it dos not mean that.
Here why if you wonder.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Headfirst Python Exercise GeorgeSears 3 1,406 Jun-11-2022, 06:36 PM
Last Post: GeorgeSears
  Wrong code in Python exercise MaartenRo 2 1,537 Jan-01-2022, 04:12 PM
Last Post: MaartenRo
  I really need help, I am new to python, I am using a book that helps me to learn JaprO 5 2,998 Nov-28-2020, 02:30 PM
Last Post: JaprO
  Python Exercise Doubt azure 4 2,681 Apr-21-2020, 01:15 PM
Last Post: azure
  New to python, having trouble with an exercise Salkay 3 2,165 Feb-18-2020, 01:42 AM
Last Post: Salkay
  Want to learn Python compilation and virtual machine IJB 3 2,643 Feb-14-2020, 02:59 PM
Last Post: IJB
  What is the bestway to learn Python? PythonRIMD 2 2,414 Jan-11-2020, 04:12 PM
Last Post: joe_momma
  Just completed Learning Python the Hard Way rxndy 5 3,547 Apr-27-2019, 01:48 AM
Last Post: rxndy
  Beginner want to learn python Keenygp 5 3,295 Apr-10-2019, 09:36 AM
Last Post: KevinBrown
  [Python] Learn words' sentiment based on labels Artificious 1 3,046 Jul-05-2018, 10:44 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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