#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0

    New to Python, looking for advice!


    Hello! My name is rhenzar and I am new to both these forums and the Python programming language! I decided to start this thread because I have both code-related questions and general programming questions, some small and some rather large. Before you ask, I am running Ubuntu 12.04 besides Windows 7 Professional, using gedit as my text editor and the Python Interpreter (v2.7.3) to handle my code. I've been following the LearnPythonTheHardWay.org guide, getting to about Exercise 19 before running into problems. Thank you for viewing this thread.

    Code-related questions:
    Am I using the "argv" module incorrectly?
    Exercise 15 in the guide uses the "argv" module. I am providing two variables (script, filename) just as the guide says (I copied the exercise directly into my file afterwards, but to no avail) and I am getting the following error in the Terminal:

    Code:
    rhenzar@ubuntu:~$/Python/PythonExercises$ python ex15.py
    Traceback (most recent call last);
      File "ex15.py", line 3, in <module>
        script, filename = argv
    ValueError: need more than 1 value to unpack
    This is the code from exercise 15:
    Code:
    from sys import argv
    
    script, filename = argv
    
    txt = open(filename)
    
    print "Here's your file %r:" % filename
    print txt.read()
    
    print "Type the filename again:"
    file_again = raw_input("> ")
    
    txt_again = open(file_again)
    
    print txt_again.read()
    The output given in the guide does not give this error, and none of the comments listed by the author's students report this problem. I believe the guide was written with v2.5.1 of the Python Interpreter. Was this module changed between those versions?

    How do I read from and append a text file?
    I'm writing a login_user.py file (also, would I call it a script?) that takes a username and password given by the user and compares it to data in a user_info.txt file, but I can't get a concrete answer for my problem with the reading and appending of the file.

    Here's the code:

    Code:
    #Python Application
    #login_user.py
    
    #Pathname Finder
    import sys, os
    pathname = os.path.dirname(sys.argv[0])
    
    print "sys.argv[0] =", sys.argv[0]
    print "full path =", os.path.abspath(pathname)
    
    class start_app:
            def open_menu(self):
                    print "Welcome to the [redacted]\n"
                    print "---------------\n"
                    print "Please log in!\n"
                                   
                    time.sleep(5)
                    
                    print "Username:"
                    username = raw_input("> ")
                            
                    f = open("user_info.txt", "r")
                    searchlines = f.readlines()
                    f.close()
                    for i, line in enumerate(searchlines):
                             if username in line: print line
                             else f = open("user_info.txt", "a") as myfile:
                                    myfile.write(username)
                             searchfile.close()
    
    start_object = start_app()
    
    start_object.open_menu(self)
    
    print "Thanks for playing!"
    
    #Note: fix for "self" feature in 'def open_menu()', etc.!
            #Fixed with "self", adding user_info.txt now
    As you can probably see, the highlighted code is incorrect, though I cannot find the correct formatting for that piece.


    Programming-related questions:
    Are there any rules of thumb that should always be abided by?
    Off of the top of my head, I think of indentation and clarity in your comments/examples. I know there are more...

    Should I make the switch to Python Interpreter v3.2.3?
    I've already installed Python v3.2.3, but I haven't looked into the changes from v2.7.3 (heck, I'm not even one with 2.7.3 yet!). Anyways, the Python.org website said that the standard on most systems is still in the 2.x range, though I've heard that there are some major changes.

    That's all I wanted to ask! I apologize in advance for any mistakes in the text (I have looked over this thread about twelve times already, so hopefully there aren't any) and, in the case that I'm just horribly bad at understanding some fundamental concept of the Python language, just tell me that I've screwed up

    Thanks in advance! I'll be watching this thread, so I will respond within a day (at max)!

    P.S. I plan on playing with code for a living (B.S. in Game Design ftw!)

    EDIT: I noticed that I used the file-reading function in the Exercise 15 example. Please note (!) that I still do not know how to implement the reading-and-appending function into my login_user.py file (file, script, whatever )
    Last edited by rhenzar; November 1st, 2012 at 11:55 PM. Reason: I overlooked something..
  2. #2
  3. Commie Mutant Traitor
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2004
    Location
    Norcross, GA (again)
    Posts
    1,804
    Rep Power
    1569
    Originally Posted by rhenzar
    The output given in the guide does not give this error, and none of the comments listed by the author's students report this problem. I believe the guide was written with v2.5.1 of the Python Interpreter. Was this module changed between those versions?
    No, nothing related to the program at hand. The issue is here on your shell command:
    Code:
    rhenzar@ubuntu:~$/Python/PythonExercises$ python ex15.py
    You seem to have misunderstood the goal of the project: to read in one argument after the name of the script file. A better description might have been to enter 'python <script> <file>', where <script> is the name of the program you are running, and <file> is the name of the file to be read. So, if you had a text file named 'foo.txt', you might enter:
    Code:
    python ex15.py foo.txt
    Note that it is more typical to handle the list argv one element at a time; argv[0] will always correspond to the name of the script, argv[1] will be the first argument for the script, and so forth.

    How do I read from and append a text file?
    I'm writing a login_user.py file (also, would I call it a script?) that takes a username and password given by the user and compares it to data in a user_info.txt file, but I can't get a concrete answer for my problem with the reading and appending of the file.
    Looking at the program, I see two major issues; when I run it, I find one more as well. One of them is indeed in the highlighted section; it is this part here specifically:
    Code:
    if username in line: print line
                             else f = open("user_info.txt", "a") as myfile:
                                    myfile.write(username)
    Here you seem to be confusing two different constructs: else: and with. More than likely, you intended this read:
    Code:
    if username in line: print line
                             else:
                                 with open("user_info.txt", "a") as myfile:
                                    myfile.write(username)
    The second problem I see is in the call
    Code:
    start_object.open_menu(self)
    This should be simply:
    Code:
    start_object.open_menu()
    You seem to have misunderstood the nature of the self argument, which is admittedly confusing to newcomers. The first argument of an instance method - usually, but not necessarily, call 'self' - is a hidden reference to the object it is invoked for. Thus, in this case, the self argument is start_object. This can be seen more readily in the alternative calling approach where you invoke the method using the class name, and explicitly pass the object as the first argument:
    Code:
    start_app.open_menu(start_object)
    The third thing I noticed was that you did not import the time module.

    Are there any rules of thumb that should always be abided by?
    Off of the top of my head, I think of indentation and clarity in your comments/examples. I know there are more...
    Indeed there are. There is a whole document full of them: PEP-8, the official Python Style Guide. You are not obliged to follow it to the letter, but it is fairly well thought-out.

    Should I make the switch to Python Interpreter v3.2.3?
    I've already installed Python v3.2.3, but I haven't looked into the changes from v2.7.3 (heck, I'm not even one with 2.7.3 yet!). Anyways, the Python.org website said that the standard on most systems is still in the 2.x range, though I've heard that there are some major changes.
    That's a tougher question that it sounds. The differences between Python 2.x and Python 3.x are significant, and Python 3.x is not backwards compatible with 2.x for most scripts. It is almost a new language, in fact. While I personally find the changes made in Python 3.x to be positive ones, and have been something of an early adopter, the fact remains that a large number of widely used libraries that haven't been upgraded yet. I personally have both installed, with Python 3.3 as the default; I call Python 2.7.3 explicitly when I use it.
    Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
    #define KINSEY (rand() % 7) λ Scheme is the Red Pill
    Scheme in Short Understanding the C/C++ Preprocessor
    Taming Python A Highly Opinionated Review of Programming Languages for the Novice, v1.1

    FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov

IMN logo majestic logo threadwatch logo seochat tools logo