2

First of all, do you know why this code doesn't work at all? When I give it the input 'when was steve jobs born' it returns nothing. Secondly I'm almost certain this can be written in a more efficent way which would take less time for the program to execute. Any ideas? Thanks!

import sys

Bill_Gates = ["bill gates","1955", "Co-founder of Microsoft"]
Steve_Jobs = ["steve jobs","1955", "Co-Founder of Apple"]
Albert_Einstein = ["albert einstein","1879", "Phycisist"]

PEOPLE = [Bill_Gates, Steve_Jobs, Albert_Einstein]

userInput = input("say something")

#checking if userInput contains the peoples name
if userInput in [j for i in PEOPLE for j in i]:
    for i in range(len(PEOPLE)):
        if PEOPLE [i][0] in userInput:
            if "when was" in userInput:
                if "born" in userInput:
                    print(PEOPLE[i][0] + "was born in " + PEOPLE[i][1])

UPDATE: Ahsanul Haque gave me just the answer I was searching for.

3 Answers 3

4

You should try to debug these kinds of problems yourself by breaking down what happens at each step. Printing the variables at each stage or playing around with a shell or debugger such as pdb are very helpful. In this case:

>>> Bill_Gates = ["bill gates","1955", "Co-founder of Microsoft"]
>>> Steve_Jobs = ["steve jobs","1955", "Co-Founder of Apple"]
>>> Albert_Einstein = ["albert einstein","1879", "Phycisist"]
>>> 
>>> PEOPLE = [Bill_Gates, Steve_Jobs, Albert_Einstein]
>>> things = [j for i in PEOPLE for j in i]
>>> things
['bill gates', '1955', 'Co-founder of Microsoft', 'steve jobs', '1955', 'Co-Founder of Apple', 'albert einstein', '1879', 'Phycisist']
>>> 'steve jobs' in things
True
>>> 'when was steve jobs born' in things
False

So the if userInput in [j for i in PEOPLE for j in i] fails because the right hand side is just a list of strings and Python isn't magic.

Your code is almost there anyway, as it works without the initial check. So this works:

for person in PEOPLE:
    if person[0] in userInput and "when was" in userInput and "born" in userInput:
        print(person[0] + " was born in " + person[1])

Note that a direct for loop works in this situation; there is no need to use range and index manually. Using the and operator is also cleaner than nested if statements.

This is plenty efficient as is unless PEOPLE has some tens of millions of elements. If you get to that stage you can look into search engine indexing techniques.

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

Comments

2

'when was steve jobs born' isn't in that list you made, whatever it is; since the outer if fails, nothing inside it gets executed.

Comments

1

What about making a Person class? It's simple, readable and easy to manage.

For example, I've written a Person class with a get_role method. Write your own get_name() and get_year_of_birth method.

class Person:
    def __init__(self, name, year_of_birth, role):
        self.name = name
        self.year_of_birth = year_of_birth
        self.role = role

    def get_role(self):
        return self.role

Bill_Gates = Person("bill gates","1955", "Co-founder of Microsoft")
Steve_Jobs = Person("steve jobs","1955", "Co-Founder of Apple")
Albert_Einstein = Person("albert einstein","1879", "Phycisist")


person_list= [Bill_Gates,Steve_Jobs,Albert_Einstein]


for person in person_list:
    print person.get_role()

Output:

Co-founder of Microsoft
Co-Founder of Apple
Phycisist

2 Comments

Thanks, just what I was looking for.
@UlrikKarlsson Glad that it helped. If you feel that the answer satisfies your question, you could consider accepting the answer, rather than mentioning it in the question. That will help the fellow users.

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.