Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Hanan Al-Mashhadani
Hanan Al-Mashhadani
7,769 Points

python program Can someone tell me what's the wrong here

def main():
    listPeopleNum = ['Toot','Steve','Ben','David','Angela','Josh', 'Jhon']
    listPhoneNumers = ['789-5634','863-7563','758-1234','555-5489','044-6666','333-0000','911-6111']

    inputDataEntry = input("Please input your friend full name ")
    for i in range(len(listPeopleNum)):
          if inputDataEntry == listPeopleNum[i]:
              print(listPeopleNum[i], listPhoneNumers[i])
          else:
              print('The name was not found')

main()

Moderator edited: Added markdown to make code properly render in the forums.

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi Hanan! I added some markdown to your question so that the code will be easily readable. You can get instructions on how to do this by checking out the Markdown Cheatsheet link at the bottom of the "Add an Answer" section. :sparkles:

7 Answers

Hi Hanan

just pass on the else statement like so

def main():
    listPeopleNum = ['Toot','Steve','Ben','David','Angela','Josh', 'Jhon']
    listPhoneNumers = ['789-5634','863-7563','758-1234','555-5489','044-6666','333-0000','911-6111']

    inputDataEntry = input("Please input your friend full name: ")
    for i in range(len(listPeopleNum)):
          if inputDataEntry == listPeopleNum[i]:
              print(listPeopleNum[i], listPhoneNumers[i])
          else:
              pass

main()

also I would lower case the input from the user and lower case each name in the list before comparison, reason being, if a user enters a name in lower case your program will fail. Example below

def main():
    listPeopleNum = ['Toot','Steve','Ben','David','Angela','Josh', 'Jhon']
    listPhoneNumers = ['789-5634','863-7563','758-1234','555-5489','044-6666','333-0000','911-6111']

    inputDataEntry = input("Please input your friend full name: ")
    for i in range(len(listPeopleNum)):
          if inputDataEntry.lower() == listPeopleNum[I].lower():
              print(listPeopleNum[i], listPhoneNumers[i])
          else:
              pass

main()

Hi Hanan

I ran your code and the result is exactly what I was expecting.

see result below

Please input your friend full name Ben                                                                              
The name was not found                                                                                              
The name was not found                                                                                              
Ben 758-1234                                                                                                        
The name was not found                                                                                              
The name was not found                                                                                              
The name was not found                                                                                              
The name was not found

So what exactly is the issue you are having ?

Hanan Al-Mashhadani
Hanan Al-Mashhadani
7,769 Points

Hi Jennifer I didn't know about that link, Thank you so much for your help :)

Hanan Al-Mashhadani
Hanan Al-Mashhadani
7,769 Points

Hi Andreas,

Thanks for replying The program should run the name and the number only one time but the program keeps showing else statement many times with the name, i changed the code many times with diffrent ways and keeps running the same result So, what's the wrong with my code ??

Hanan Al-Mashhadani
Hanan Al-Mashhadani
7,769 Points

it's a good idea, but by this way, it like we cancel else statement, else statement should work when the user type different name from the names above the program should print "The name was not found"

Hi Hanan

I changed your code a bit, see blow

def main():
    listPeopleNum = ['Toot','Steve','Ben','David','Angela','Josh', 'Jhon']
    listPhoneNumers = ['789-5634','863-7563','758-1234','555-5489','044-6666','333-0000','911-6111']

    inputDataEntry = input("Please input your friend full name: ").title()
    if inputDataEntry not in listPeopleNum:
      print("Name was not found!")
    else:
      for i in range(len(listPeopleNum)):
        if inputDataEntry == listPeopleNum[i]:
          print(listPeopleNum[i], listPhoneNumers[i])



main()

All I have done is title case the input, check if the name is in the list of names, if not, print out 'Name was not found' else iterate through the list. Hope this helps

Hanan Al-Mashhadani
Hanan Al-Mashhadani
7,769 Points

That's brilliant, I didn't know that we could use 'not in' in for statement, very smoothly and very straight forward Thanks you so much :)