omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Interactive interpreter

    Pythonista
    3
    6
    3014
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • karthikmaiya
      karthikmaiya last edited by

      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.

      1 Reply Last reply Reply Quote 0
      • ccc
        ccc last edited by

        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}
        
        1 Reply Last reply Reply Quote 0
        • ccc
          ccc last edited by

          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?

          1 Reply Last reply Reply Quote 0
          • karthikmaiya
            karthikmaiya last edited by

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

            1 Reply Last reply Reply Quote 0
            • karthikmaiya
              karthikmaiya last edited by

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

              1 Reply Last reply Reply Quote 0
              • dgelessus
                dgelessus last edited by

                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)
                
                1 Reply Last reply Reply Quote 0
                • First post
                  Last post
                Powered by NodeBB Forums | Contributors