2

I'm trying to have the user input a telephone number, then the program will open a text file, convert it to a list, search the list and if the phone number is found it will return the rest of the line (which contains the address for that phone number.

I can only get it to return either "phone number not found" or the line but it does it for every single line so I end up with an output like this:

phone number not found
phone number not found
0121 254132 18 Springfield Road
phone number not found
phone number not found

for line in phonenumbers:

    if number in line:
        print (line)
    else:
        print ("phone number not found")

I know it's because I've put for line in phonenumbers but don't know how to not do it for every line.

6
  • Is the phone number the only data in the file Commented Jul 14, 2015 at 12:06
  • The data file looks like this: Commented Jul 14, 2015 at 12:43
  • 0121254132 18 Springfield Road Commented Jul 14, 2015 at 12:44
  • Sorry don't know how to get another line in these comments but each phone number has a street address next to it on the line, then the next line has the next phone number and street address and so on. Commented Jul 14, 2015 at 12:45
  • So there will be two kind of data one is phone number and other will be phone number not found right Commented Jul 14, 2015 at 12:46

2 Answers 2

4

try the following:

for line in phonenumbers:    
    if number in line:
        print (line)
        break
else:
    print ("phone number not found")

the else is part of the for loop and will only execute if you didn't break out of the for loop.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the help. When I do this it does print the correct line but also prints "phone number not found" underneath too.
Are you sure you indented the else clause with the same indent as the for ? And did you notice the break clause inside the if?
Thank you! Yes, I was being an idiot and didn't notice the break. So the break just breaks out of the loop after finding one true result? You're a star, thanks.
0

Why do you not just drop the else statement. It should look something like:

for line in phoneNumbers:
  if phone in line:
     print line

If you want a result in case that it was not found you could use a flag. moreover you can just use:

print [line for line in phonenumbers if number in line]

2 Comments

Sorry, I'm quite new to python. What's a flag?
It is not python specific that's just you set a variable to be equal to true or 1 when a certain event happens and then after the event might have happened you check the value of that variable it's called a flag since when you set that variable to 1 or true it is said that you "raise a flag". You could see that in some low level languages being used a lot as they lack another way of being notified for things but you can pretty much use it anywhere you find suitable. Since you're new to python I'd suggest that you have a look at list comprehensions they do magic stuff once you understand them!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.