0

So Im teaching myself how to program. So far I am going nowhere.

I have a code that from a line in a textfile, I put each word of that line in a list. Let's say the list is info =[john,guard,single]. I have successfully loaded the words into the list but have some problems with my next task. i want to get each word and use it in a sentence all i have done so far is

for word in info:
    print "My name is ",word[1]
    print "My job is " ,word[2]
    print "I am ",word[3]

but all i get is My name is o My job is h I am n can someone please help me?

ok so here's the code:

def loginfo(self):
    infolist = []
    with open('user.txt') as f:
        f = f.readlines()
        for line in f:
            if str(self.neim.get()) in line:
               if str(self.posse.get()) in line:
                  for word in line.split():
                      infolist.append(word)

    for word in infolist:
        print word[]
kylieCatt
  • 10,672
  • 5
  • 43
  • 51
jc13
  • 85
  • 3
  • 5
  • 12
  • 3
    It looks like you are accessing the strings individually instead of the list. Can post your code please? – kylieCatt Mar 28 '14 at 19:19
  • What is `infolist`? Is it a single dimensional list with 'name', 'job' and 'status' being repeated? Or is it a 2D List? – Sukrit Kalra Mar 28 '14 at 19:27
  • Well, I planned that the infolist be empty first. Then the code will search the textfile for a specific word that i will input. If a line contains that word then the each word of the line goes into the infolist – jc13 Mar 28 '14 at 19:35

3 Answers3

5

You could do something along these lines:

li=['My name is {}', "My job is {}", "I am {}"]
info=['john','guard','single']

for x, y in zip(li, info):
    print x.format(y)

Prints:

My name is john
My job is guard
I am single

Or, just use a single template:

print 'My name is {}\nMy job is {}\nI am {}'.format(*info)
dawg
  • 98,345
  • 23
  • 131
  • 206
  • thank you! Will this still work if i pass it as a parameter in a function? Like login(*infolist) – jc13 Mar 28 '14 at 19:36
  • Well, yes, depending on how the function is written. This: `f1(1,2,3)` is the same as `f2(*[1,2,3])` but not the same as `f3([1,2,3])`. `f3` has 1 argument -- a list with 3 elements; `f1` has 3 arguments and `f2` has a list that is expanded into 3 arguments. See Python docs on [functions](http://docs.python.org/2/tutorial/controlflow.html#defining-functions) and [what does the * do](http://stackoverflow.com/a/36908/298607) – dawg Mar 28 '14 at 19:45
1

You don't need that for loop. You can simply:

print "My name is ",info[0]
print "My job is " ,info[1]
print "I am ",info[2]

In your loop you iterating over the list and assigning each string inside it to the variable word. you were then accessing the word with the index method []. Which is why you were getting individual letters instead of words. Also computers start counting at 0 not 1 so the indexes go:

[0,1,2,3]

So when you use a for loop like this:

for word in info:
    print(word[0])

What happens is:

  • The first element in the list is accessed which in your case is the string John
  • Next you call the index method on word word[0]
  • That will access and return the character in the 0th index which is the first character in the string
  • It will do that whole set of instructions for every element in the list
kylieCatt
  • 10,672
  • 5
  • 43
  • 51
0

for word in info: is a loop. Its body (the indented part) will execute once for each element of info.

You could access info elements by index, as other answers suggest. You can also unpack the list into reasonably-named variables:

name, job, marital_status = info # the unpacking: 3 variables for 3 list items
print "My name is", name, "my job is", job, "I am", marital_status

The loop comes in handy when you have a list of people to process:

people = [  # a list of lists
  ["Joe", "painter", "single"],
  ["Jane", "writer", "divorced"],
  ["Mario", "plumber", "married"]
]

for person_info in people:
  print "Name:", person_info[0], "job:", person_info[1], "is", person_info[2]

It is idiomatic to unpack nested items right in the loop:

for name, job, marital_status in people:
  print "Person named", name, "works as a", job, "and is", marital_status
9000
  • 39,899
  • 9
  • 66
  • 104