如何使用用户输入引用字典并打印出与字典中的所述单词相关联的相应值?比如说我输入"值"并在字典"价值"与数据1相关联;我怎么能打印或携带它再次使用?
class Rating(object):
def enter(self):
exit(1)
class userInput():
def inputReview(self):
review = ""
print "Welcome to our review system! Please input review text below."
review = raw_input("> ")
print ""
goodWords = {
'value': 1,
'Solid': 2,
'faster': 3,
'great': 4,
'beast': 5
}
badWords = {
'slow': 1,
'aftermarket': 2,
'weak': 3,
'dropping': 4.,
'freezes': 5,
}
print goodWords.values()
print ""
if review in goodWords:
print goodWords[review]
else:
exit(1)
class review():
pass
a = userInput()
a.inputReview()
答案 0 :(得分:0)
您需要创建一个实例并在实例上调用该方法:
class userInput():
def inputReview(self):
print "Welcome to our review system! Please input review text below."
review = raw_input("> ")
print ""
goodWords = {
'value': 1,
'Solid': 2,
'faster': 3,
'great': 4,
'beast': 5
}
badWords = {
'slow': 1,
'aftermarket': 2,
'weak': 3,
'dropping': 4.,
'freezes': 5,
}
print goodWords.values()
print ""
if review in goodWords:
print goodWords[review]
else:
exit(1)
foo = userInput() # create instance of the class
foo.inputReview() # call method on the instance
演示:
In [5]: foo = userInput()
In [6]: foo.inputReview()
Welcome to our review system! Please input review text below.
> value
[2, 5, 4, 3, 1]
1
除非你打算添加更多方法,否则我会把它变成一个函数。