• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 278
  • Last Modified:

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"

0
darrenteo82
Asked:
darrenteo82
  • 3
  • 3
  • 2
  • +1
1 Solution
 
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:
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
 
cjjcliffordCommented:
D'oh!
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now