我試圖在Python中創建類的實例時返回字典的值,但我一直收到「無」返回。嘗試在Python中返回字典的值時發出「無」
我對Python很陌生,所以我確信這個答案很簡單。
運行以下後:
class TestTwo(object):
def __init__(self):
self.attributes = {
'age': "",
'name' : "",
'location': ""
}
def your_age(self):
self.attributes['age'] = raw_input("What is your age? > ")
self.your_name()
def your_name(self):
self.attributes['name'] = raw_input("What is your name? > ")
self.your_location()
def your_location(self):
self.attributes['location'] = raw_input("Where do you live? > ")
self.results()
def results(self):
print "You live in %s" % self.attributes['location']
print "Your number is %s" % self.attributes['age']
print "Your name is %s" % self.attributes['name']
d = self.attributes
return d
output = TestTwo().your_age()
print output
我結束了這一點:
MacBook-Pro-2:python johnrougeux$ python class_test.py
What is your age? > 30
What is your name? > John
Where do you live? > KY
You live in KY
Your number is 30
Your name is John
None
而是 「無」,我所期待的「{ '年齡' 的: '30',「名':'John','location':'KY'}「
我錯過了什麼?
這是一個完全倒退的方法來使用類。獲取構造對象所需的信息,然後構造它。類不應該負責獲取該信息,而只是爲了使用它。 – 2011-12-23 06:31:56