Where Does "raw_input" Write To?
Now that I have the Python plug-in running under IntelliJ 8.1, I'm working my way through "Core Python" by Wesley Chun and "Programming Collective Intelligence" by Toby Segaran.
In "PCI" chapter 2, "Making Recommendations", there's a simple example showing how to calculate similarities between movie ratings by a number of critics. The problem runs just fine if coded from the book. But I decided to try and change it a bit to reinforce some of the ideas from "Core Python". I made minor modifications to break the problem into three .py files, all in a module named "recommendations":
recommendations.py:
# A dictionary of movie critics and their ratings
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,
'You, Me and Dupree' : 3.5,
'The Night Listener' : 3.0
},
'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,
'Superman Returns' : 4.0,
'You, Me and Dupree' : 2.5,
'The Night Listener' : 4.5
},
'Mick LaSalle' :
{
'Lady in the Water' : 3.0,
'Snakes on a Plane' : 4.0,
'Just My Luck' : 2.0,
'Superman Returns' : 3.0,
'You, Me and Dupree' : 2.0,
'The Night Listener' : 3.0
},
'Jack Matthews' :
{
'Lady in the Water' : 3.0,
'Snakes on a Plane' : 4.0,
'Superman Returns' : 5.0,
'You, Me and Dupree' : 3.5,
'The Night Listener' : 3.0
},
'Toby' :
{
'Snakes on a Plane' : 4.5,
'Superman Returns' : 4.0,
'You, Me and Dupree' : 1.0,
},
}
similarity.py:
def distance(preferences, person1, person2) :
"Calculate the similarity of two preferences using Euclidian distance sum of squares"# Find all the items shared between person1 and person2
sharedItems = {}
for item in preferences[person1] :
if item in preferences[person2] :
sharedItems[item] = 1;# If no shared items, return zero
if len(sharedItems) == 0 : return 0# Calculate sum of squares of distances between shared items
sumOfSquares = sum((preferences[person1][item]-preferences[person2][item])**2
for item in preferences[person1] if item in preferences[person2])return 1/(1+sumOfSquares)
driver.py:
import recommendations
import similarityprint recommendations.critics
#critic1 = raw_input('1st critic: ')
#critic2 = raw_input('2nd critic: ')critic1 = 'Lisa Rose'
critic2 = 'Gene Seymour'print similarity.distance(recommendations.critics, critic1, critic2)
If I run driver.py as is, I get the following output in my console, which is correct:
C:\Tools\Python-2.5\python.exe "driver.py"
{'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}, '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}, '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}, 'Lisa Rose': {'Lady in the Water': 2.5, 'Snakes on a Plane': 3.5, 'Just My Luck': 3.0, 'Superman Returns': 3.5, 'The Night Listener': 3.0, 'You, Me and Dupree': 2.5}, 'Toby': {'Snakes on a Plane': 4.5, 'Superman Returns': 4.0, 'You, Me and Dupree': 1.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}}
0.148148148148Process finished with exit code 0
However, if I substitute the commented calls to raw_input the code simply sits without writing the prompt. Why isn't raw_input writing to my console? How can I tell it to do so? Thanks.
请先登录再写评论。