Forum Archive

Interactive interpreter

karthikmaiya

I have asked previous questions but this time is different. I am trying to teach myself about collective intelligence and i have a book that walks me theough the different parts. However i run into this interesting issue. Here is what the book says.
The first thing you need is a way to represent different people and their preferences. In Python, a very simple way to do this is to use a nested dictionary. If you’d like to work through the example in this section, create a file called recommendations.py, and insert the following code to create the dataset:

# A dictionary of movie critics and their ratings of a small
     # set of movies
     critics={'Lisa Rose': {'Lady in the Water': 2.5, 'Snakes on a Plane': 3.5,
      'Just My Luck': 3.0, 'Superman Returns': 3.5, 'You, Me and Dupree': 2.5,
      'The Night Listener': 3.0},
     'Gene Seymour': {'Lady in the Water': 3.0, 'Snakes on a Plane': 3.5,
      'Just My Luck': 1.5, 'Superman Returns': 5.0, 'The Night Listener': 3.0,
      'You, Me and Dupree': 3.5},
     'Michael Phillips': {'Lady in the Water': 2.5, 'Snakes on a Plane': 3.0,
      'Superman Returns': 3.5, 'The Night Listener': 4.0},
     'Claudia Puig': {'Snakes on a Plane': 3.5, 'Just My Luck': 3.0,
      'The Night Listener': 4.5, 'Superman Returns': 4.0,
      'You, Me and Dupree': 2.5},
     'Mick LaSalle': {'Lady in the Water': 3.0, 'Snakes on a Plane': 4.0,
      'Just My Luck': 2.0, 'Superman Returns': 3.0, 'The Night Listener': 3.0,
      'You, Me and Dupree': 2.0},
     'Jack Matthews': {'Lady in the Water': 3.0, 'Snakes on a Plane': 4.0,
      'The Night Listener': 3.0, 'Superman Returns': 5.0, 'You, Me and Dupree': 3.5},
     'Toby': {'Snakes on a Plane':4.5,'You, Me and Dupree':1.0,'Superman Returns':4.0}}

In the interactive interpreter follow these commands`
Type "help","copyright", "credits" or "license" for more information.

from recommendations import critics
critics['Lisa Rose']['Lady in the Water']
2.5
critics['Toby']['Snakes on a Plane']=4.5
critics['Toby']
{'Snakes on a Plane':4.5,'You, Me and Dupree':1.0}
However when i try to do what it says it gives me an error that critics cannot be found.

ccc

When posting code that you want formatted, you need to put a blank line before ```python unless it on the very first line of your post.

Does from recommendations import critics fail? It worked for me. As always, make sure that your file is not called recommendations.py.py!!

Equals signs need to be added to both lines:

critics['Lisa Rose']['Lady in the Water'] = 2.5  # and...
critics['Toby'] = {'Snakes on a Plane':4.5,'You, Me and Dupree':1.0}
ccc

At the upper right of your post above, you will see a link called edit. Click that link and update your post to have a blank line before the ```python as well as a closing three tick marks so the structure of the code is clearer. On which line are you getting the critics not found error? What book are you using?

karthikmaiya

It gives a reccomendations not found within the interpreter. I am using Programming Collective Intelligence
From oreilly thanks for helping ccc

karthikmaiya

How did u make it correctly import what exactly did u do? I still cant get it to work.

dgelessus

Make sure that the "recommendations.py" file is named "recommendations" by Pythonista. The app automatically puts .py at the end, so if you call a script "recommendations.py" it will be named "recommendations.py.py".

In the console you can then type

import recommendations

If it says nothing afterwards, everything worked. You can then access the critics using recommendations.critics, for example:

print(recommendations.critics)