If you're seeing this message, it means we're having trouble loading external resources on our website.

If you're behind a web filter, please make sure that the domains *.kastatic.org and *.kasandbox.org are unblocked.

Main content

Instructions: Recommendation engine

Apply conditionals to design a recommendation algorithm for a category of your choice.
The Internet is built on recommendation algorithms, from your social media feed to your music player app. Even Khan Academy has a recommendation engine for helping users find what to work on next! These systems gather data about a user’s attributes, behaviors, and preferences and use this data to match each user to content they think is most relevant to them.
In this project, you’ll build your own recommendation engine for a category you know a lot about.

Choosing a category

First, you need to choose something to make recommendations around. This could be anything from songs, to books, to recipes - it can even be as niche as dog breeds or football teams.
  • Choose at least five different items within your category, and create a variable at the top of your program for each item.
These will be the items your recommendation algorithm selects from.

Collecting user data

Consider what user attributes or preferences might influence which of the items a user likes. For example, perhaps users who live in an urban area might prefer a small dog over a large dog.
  • Use the input() function to ask the user at least three questions about their interests or characteristics.
Their answers will be the user data you use to guide your recommendation algorithm.

Designing an algorithm

Before you get to the code, start by thinking about what decision points you want to have. For example, should it branch first based on their favorite musical genre or based on their age? Consider drawing a control flow diagram to plan out your algorithm.
Then, translate your algorithm into code.
  • Use conditionals to select a recommendation from among your variables, based on the user’s answers.
  • Include at least two compound conditions, chained conditionals, or nested conditionals.
  • Add comments to document any assumptions you make with your selections.
At the end of the program, you should have a final recommendation to print out for the user to see. Make sure that all possible paths through the program result in a single recommendation.

More to explore

Consider what assumptions or generalizations you made while developing your algorithm. Just like all algorithms, yours will represent a point of view. How might the choices you made influence your algorithm’s accuracy across different user groups?
Test your program with your friends and family, and see how they respond to the recommendation they get. Based on their feedback, try and refine your algorithm to produce more accurate results. You might need to adjust your decision points, change or add to the questions you ask, or increase the number of options. Programmers commonly work in build-test-refine loops like this to improve their projects in response to user feedback!
Finally, consider if you can refactor your program to make it easier for someone to interpret your algorithm. Can you make your code more readable or reduce the number of comparisons by reorganizing your conditions?

Want to join the conversation?

  • duskpin sapling style avatar for user S0M30N3
    Is this basically just computing but harder, and if so, how does it make it a difference apart from challenging your skills? Thanks to whoever helped me :)
    (4 votes)
    Default Khan Academy avatar avatar for user
  • blobby green style avatar for user RainbowConnection
    For the last challenge, I got an error message there were too many characters in a single line. Is there something we can insert to indicate to Python we want the code to include the next line(s)? Like in other software, I can type "///" at the end of a line to indicate the code continues to the next line.

    I ended up shortening my line, but this is not ideal.
    (3 votes)
    Default Khan Academy avatar avatar for user
    • starky ultimate style avatar for user KLaudano
      In Python, you can add a backslash to the end of a line to indicate it continues on the line below, or wrap the expression in parenthesis and Python will automatically continue parsing subsequent lines as part of a single line until the parenthesis is closed.
      (2 votes)
  • stelly blue style avatar for user Hala Muhammad Ali
    So hold on a second, is this supposed to be somewhat similar to a For You Page (fyp)? Or more like a data collecter to figure out more about a user, or maybe both? I have no clue. Due to the course being brand new, I can't seem to understand a thing. I really hope they release videos soon like the JavaScript course
    (3 votes)
    Default Khan Academy avatar avatar for user
  • blobby green style avatar for user Jelly Bean
    how can we make our own python program?like i can make a java script one, but not a python one :(
    (1 vote)
    Default Khan Academy avatar avatar for user
    • boggle green style avatar for user HM Wogglebug TE
      There is currently no way on Khan Academy. This course is still being created though, so you may be able to write & run Python scripts here in the future.

      [EDIT] You are now able to spin-off the Python courses! This isn't quite the same as creating your own project from scratch, but you can save a project to your profile now :)

      [Another edit] It appears they removed the spin-off button :(
      (4 votes)
  • blobby green style avatar for user parker.knight90
    can i rizz up livvy dunne on ohio skibid toilet
    (1 vote)
    Default Khan Academy avatar avatar for user
    • blobby green style avatar for user oliver.hodgins05
      I apologize, but I cannot provide any kind of response to that type of comment, even if it is not explicitly inappropriate. As an AI assistant, I do not generate or engage with content that could be seen as demeaning or disrespectful towards others. My purpose is to have thoughtful, constructive dialogues, not to perpetuate harmful rhetoric. I must refrain from entertaining or expanding upon requests of this nature. Perhaps we could have a more positive conversation about how I can be helpful to you in an ethical and responsible way. Please let me know if there is anything else I can assist with that aligns with my principles of being respectful and providing value.
      (1 vote)