Sign in to follow this  

Help me, i can't understand well :)

This topic is 1212 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,

Please ,Help me i can't understand the code well smile.png

i can't understand everything

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)
Edited by koka282

Share this post


Link to post
Share on other sites

I'm not the best person to answer the question, but I don't even know what you're asking. 

-Did you see this group of code, and don't know what it means?

-Are you writing something, and this is not working?

Share this post


Link to post
Share on other sites

i wrote it the topic & i continue trying to to understand it i was going to see at night my understanding was right or not but now i i think i understand i take long time to understand but i think that's not problem right ?&i will try to write the code with myself it will be hard but i will write it :)

Share this post


Link to post
Share on other sites

Yep. You should take it slow. There is no hurry.

 

 

i wrote it the topic & i continue trying to to understand it i was going to see at night my understanding was right or not but now i i think i understand i take long time to understand but i think that's not problem right ?&i will try to write the code with myself it will be hard but i will write it

 

Best way to be sure, that you understand a piece of code, is to modify it, so it does something different. If it does what you thought it would do, you can assume that you understand that particular piece of code.

Share this post


Link to post
Share on other sites
def rewind(f):
    f.seek(0) 

can you explain ??

 

if i didn't type it i can print the file again why !? 

it close the file or what 

need explain please smile.png

current_line = 1
print_a_line(current_line, usingfil)

current_line = current_line + 1
print_a_line(current_line, usingfil)

current_line = current_line + 1
print_a_line(current_line, usingfil)

if current line = 2 

it print the second line how ???!!!!!!! 

Edited by koka282

Share this post


Link to post
Share on other sites

You're probably too young to remember cassette tapes and VCR tapes, but if not, imagine a file like an old fashioned tape.

 

When you open a file, it's like starting to play a tape from the start.  After you've read some of the tape, if you want to go back to the start of the tape, you have to tell it to rewind to the start (ie, go back to position 0).  That's what file.seek(0) does, it's rewinding the file back to the start of the file.

 

But, as I have suggested before, you need to look at a line of code, and try and explain what YOU think it is doing.  If you have no idea, then you need to go back to the start of the tutorial, and start over, READ the material, understand what's happening before you go on to the next exercise.

Share this post


Link to post
Share on other sites

But, as I have suggested before, you need to look at a line of code, and try and explain what YOU think it is doing.  If you have no idea, then you need to go back to the start of the tutorial, and start over, READ the material, understand what's happening before you go on to the next exercise.

I'm trying to do like what u say

i think i know now what do u mean with tape smile.png

but in another script i print the file two times with .seek(0)

you know why that ??! 

 

Thank you bro smile.png))


if current line = 2 
it print the second line how ???!!!!!!! 
 

 

and if u know the answer pls tell me smile.png

Edited by koka282

Share this post


Link to post
Share on other sites

koka282,

 

It is a bit hard to understand what you are asking for help with. As others have suggested you should go at a pace through the tutorials where you can understand all of the concepts being taught. Do not move on to other lessons until you understand the concepts of the previous lessons. Practice using those concepts in your own programs.

 

When you call print_a_line for the second time it takes two parameters. The first parameter is current_line which started with a value of 1 and was incremented to a value of 2 right before the second print_a_line call. The second parameter is the file to read. When you call the print_a_line function then you are printing the first parameter (the value 2) and the return from calling the readline method on the python File object passed into parameter 2 (current_file). The readline method will return the next line of the file object when called. Every time you call it you will get the next line in the file.

 

You can think of the seek(0) like a "reset" that allows you to start reading from the start of the file.

 

Keep in mind that having us tell you the answers might not be the best way for you to learn the concepts. You might want to try to really try to understand before you ask for help.

 

Also when asking for help, please try to do the following:

 

1. Ask specific questions. You are more likely to receive help if you have a specific question that can be quickly answered. Broad questions are difficult to answer and may not receive a swift and/or helpful response.

 

2. Use complete sentences. I understand that English is not everyone's native language... and I also understand that not everyone will write perfect english on a forum... however putting some care into your sentences will aide in making your posts readable. Also writing as well as possible will help show your maturity and that you are serious in asking for help.

 

3. Understand that it is not just you who are receiving help. These topics stay around for quite a while. It is very possible that someone in the future may find this thread and have similar questions. Following the previous two pieces of advice can help people in the future as well.

Edited by shadowisadog

Share this post


Link to post
Share on other sites

Hi @koka282,

I took a look at the tutorial you're following and the author targets Python 2, which is very well documented. I have two suggestions:

- When you see a new term, look it up in the glossary;

- When you see a new function, look it up in the index.

 

While asking questions is fine, pay attention to what the author himself states on Exercise 0 of the tutorial:

Finding Things on the Internet

A major part of this book is learning to research programming topics online. I'll tell you to "search for this on the internet," and your job is to use a search engine to find the answer. The reason I have you search instead of just giving you the answer is because I want you to be an independent learner who does not need my book when you're done with it. If you can find the answers to your questions online then you are one step closer to not needing me, and that is my goal.

Thanks to search engines such as Google you can easily find anything I tell you to find. If I say, "search online for the python list functions" then you simply do this:

  1. Go to http://google.com/
  2. Type: python list functions
  3. Read the websites listed to find the best answer.

 

Before you can be a beginning game developer, you have to get past being a beginning programmer. You should do your best to thoroughly research and try to find the answers yourself before you ask questions in any forum, not because people aren't willing to help you (which they ARE, GameDev.net has a FANTASTIC community, one of the best I've seen since I started using the internet in '96), but because it will be better for you in the long run.

Share this post


Link to post
Share on other sites

i care about time

i hate take long time to understande

i want to finish it & do my own programs sad.png

That's wrong i know sad.png

Thank you all smile.png

 

If you hate taking a long time to understand things, then programming will be pretty frustrating to you.

You see, a very important technique for becoming a better programmer is reading code written by other people. But even if you understand every single syntax element and know what each function does, you still have to understand the algorithms and the design of the application. Not to mention things like math and physics.

Can a dedicated, hard-working person get a good grasp of a language like Python in a few months? Sure. But it takes *years* to become a decent software developer who can actually write production-level code.

If you're willing to put a *lot* of time and effort and patience into it, those years will be an incredible journey. But it will still take years. While there are those who claim you can Teach Yourself Python in 24 Hours, it would be better to Teach Yourself Programming in Ten Years.

Share this post


Link to post
Share on other sites

 

i care about time

i hate take long time to understande

i want to finish it & do my own programs sad.png

That's wrong i know sad.png

Thank you all smile.png

 

If you hate taking a long time to understand things, then programming will be pretty frustrating to you.

You see, a very important technique for becoming a better programmer is reading code written by other people. But even if you understand every single syntax element and know what each function does, you still have to understand the algorithms and the design of the application. Not to mention things like math and physics.

Can a dedicated, hard-working person get a good grasp of a language like Python in a few months? Sure. But it takes *years* to become a decent software developer who can actually write production-level code.

If you're willing to put a *lot* of time and effort and patience into it, those years will be an incredible journey. But it will still take years. While there are those who claim you can Teach Yourself Python in 24 Hours, it would be better to Teach Yourself Programming in Ten Years.

 

U are right bro :)

but i don't hate taking lot of time

but i care i want to learn python &networking&(php)

but u are right i should be Impatient person

Thank you :)

Share this post


Link to post
Share on other sites

I have never seen a single line of Python in my whole life, but to me it seems like the whole program is defining three of something that may be a macro/definition/function that

 

1. prints the content of a file (from the current file position, despite the name saying "print_all")

2. seeks to the beginning of the file (rewind)

3. prints the content of a file until it reaches a newline

 

Googling for "python def" reveals the following page, showing that I am right: it is defining three functions doing those things: https://docs.python.org/release/1.5.1p1/tut/functions.html

 

Defining a function means that you are telling Python about them, but they don't do anything until you call the functions themselves.  That is done in the main part of the program:

 

1. Opens a file (it is now at the beginning of the file)  This returns a handle to the file that you use to do further communication with the file.  The input to this function seems to be the input that you provided when you are calling the Python script, and is the name of the file.

 

2. Calling the function to print the content of the file (which is all of the file since you just opened it, also sort of explaining why the function is named "print_all")  You are sending in the handle to the file as a parameter.  If you were at a different position in the file, it would not have printed all, but everything from the current file position until the end of the file.

 

3. Calling the function to rewind the file: that means seek to file position 0, the beginning of the file

 

4. Calling the function to print one line of the file.  This is done three times, while incrementing a counter that is irrelevant to the file itself, but it will print as line 1 <something>, 2 <something>, etc...  If you had been at a different file position, it would still be printing line 1.., 2.., 3.., even if it really were lines 10.., 11.., 12.. it was printing.

 

In between these things, there are print that outputs what is actually happening.

 

That is about it.  

 

Note: there is probably missing the closing of the file, which I would assume should be there, otherwise you would run out of file handles in the long run if you handled a lot of files this way.

 

An assignment for you:

 

Can you think of a way to make print_all() not actually print everything?

Edited by aregee

Share this post


Link to post
Share on other sites

This topic is 1212 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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

Sign in to follow this