python prob

hi, i woud like to ask a question with my code below everytime i enter the name and age it doesn't work .... any help is appreciated thanks .....

import string

def test():

   name = raw_input("Enter your name: ")
   age = raw_input("Enter your age: ")

   for line in open("testfile", "r").readlines():
      testList = string.split(line, ':')

      if(testList[0] !=name or testList[1] != age):
          print "error msg"
      else:
         print"success"

darrenteo82Asked:
Who is Participating?
 
cjjcliffordConnect With a Mentor Commented:
readlines() on a file object maintains the line separator (i.e. '\n'), so for example, a file containing "john doe:23" will actually be readlines() into ['john doe:23\n'], which splits into ['john doe', '23\n'] - the "age" part still has the newline!

So, read the lines as follows:

for line in [ line.strip() for line in open( 'testfile' ).readlines() ]:
    # No line separator at end of line!!!

or you could do the strip() just before the split:

testLisst = string.split( line.strip(), ':' )

Note that string.split() is the old way, you should use the spilt() method on the line object itself, i.e.

testList = line.strip().split( ':' )

this is more readable...
0
 
RichieHindleCommented:
The lines returned by readlines() include the trailing newline, so for the age you're comparing "20" with "20\n".  Use string.strip() like this:

   for line in open("testfile", "r").readlines():
      line = string.strip(line)
      testList = string.split(line, ':')

to remove the newlines and it will work as you expect.
0
 
cjjcliffordCommented:
D'oh!
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
darrenteo82Author Commented:
thanks guys I have another question if my file has
john doe:23: dick smith:34
george bush:44   <---- next line

how do i do it so it reads the names and age for each person in the file ? thanks
0
 
cjjcliffordCommented:
you could create another list that stores all the individual person's lists...

# assumes "lines" is the readlines() of the file, after newline stripping that is!
bigList = [ line.split( ':' ) for line in lines ]

0
 
RichieHindleCommented:
This script:

import re

TEST = """\
john doe:23: dick smith:34
george bush:44
"""

for match in re.finditer(r'[: ]*(.*?):(\d+)', TEST):
    print match.group(1), "is", match.group(2)

prints this output:

john doe is 23
dick smith is 34
george bush is 44

Hopefully that answers your second question.
0
 
darrenteo82Author Commented:
i meant if i were to look up and compare the age and name in the file of every line to the one that the user has input ...... so that if the user inputs name george bush and age 44 it is success .... or name dicksmith and age 34 print success thanks ....

 for line in open("testfile", "r").readlines():
      testList = line.strip().split( ':' )
 
     for i in testList:
         if(testList[i] == name and testList[i] == age):

would it be something like the above ?
0
 
darrenteo82Author Commented:
hmm i think it's getting complicated ..... probably should just have a name and an age on every line ....... and check it with the user input .....
0
 
ramromconsultant Commented:
darrenteo82:

1) Regarding your original question: please in the future instead of saying "does not work" give us the input, the expected behavior and the actual behavior.
Example:
Input file:
Paul Revere:24
John Smith:43
Interaction:
Enter your name: Paul Revere
Enter your age:24
Expected:
"success"
Actual:
"error msg"
"error msg"

2) Regarding "thanks guys I have another question". We prefer that you accept an answer for the original question and open another question when you have another question. As it stands now there are 2 questions here, both have been addressed with useful answers. Please accept an answer or ask for clarification.

Our primary role here is to provide help with Python, not with the process of analysis and design.

Bob Gailer
Page Editor
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.