Playing about with python I thought about trying to write a program that gets a simple text file eg. hello %s, and using an adress book, loops through it, changing the %s every time into the next name in the adress book, using a queue.

But it just doesn't want to work.

AdressBook = ['Eve', 'Bob', 'Mike', 'Tom', 'Jane']

for x in AdressBook:
    print x, '\n'

f = open('C:\Documents and Settings\Henry\My Documents\Python_Stuff\Letter.txt', 'r+')

for a in AdressBook:
    x = AdressBook.index(0)
    for i in f:
        print i % (x)
    AdressBook.pop(0)

When I run this, it says:

Traceback (most recent call last):
  File "C:/Documents and Settings/Henry/My Documents/Python_Stuff/AB_LF.py", line 10, in <module>
    x = AdressBook.index(0)
ValueError: list.index(x): x not in list

Confuzzled...

Recommended Answers

All 2 Replies

AdressBook = ['Eve', 'Bob', 'Mike', 'Tom', 'Jane']

for x in AdressBook:
    print x, '\n'

f = open('C:\Documents and Settings\Henry\My Documents\Python_Stuff\Letter.txt', 'r+')

for a in AdressBook:
    x = AdressBook.index(0)
    for i in f:
        print i % (x)
    AdressBook.pop(0)

Confuzzled...

I'm confuzzled by your code. Do you know what list.index(x) does? It returns the index of item x in the list. If you were hoping to retrieve the first item of the AdressBook list (by the way you spelled Address wrong), you should be using slicing AdressBook[0] or even simply use pop, as that will return the value that gets removed.

So basically I'd change your final loop like so:

for a in AdressBook:
    x = AdressBook.pop(0)
    for i in f:
        print i % (x)

But this raises the question, why are you popping from the Address book? Why not just simply iterate over it and use those values? ie,

for addy in AddressBook:
    print i % addy

I fail to see the point of iterating over your file (which I presume contains a single line containing '%s'). What is it that you're trying to achieve?

was working through a lot of tuts, and just wanted to get a clear understanding of looping and list.pop(). I can see it's not very logical now though. lol
Still very very very noobish.

thanx tho

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.