类不输出参数/做任何事情? (蟒蛇)

时间:2016-12-20 09:41:14

标签: python class oop object

{   "results" : [
  {
     "address_components" : [
        {
           "long_name" : "Artsan Towers",
           "short_name" : "Artsan Towers",
           "types" : [ "premise" ]
        },
        {
           "long_name" : "4",
           "short_name" : "4",
           "types" : [ "street_number" ]
        },
        {
           "long_name" : "Trichy Road",
           "short_name" : "Trichy Rd",
           "types" : [ "route" ]
        },
        {
           "long_name" : "Near Ramanathapuram Signal, Behind SS Palamuthir Nilayam",
           "short_name" : "Near Ramanathapuram Signal, Behind SS Palamuthir Nilayam",
           "types" : [ "neighborhood", "political" ]
        },
        {
           "long_name" : "Ramasamy Nagar",
           "short_name" : "Ramasamy Nagar",
           "types" : [ "political", "sublocality", "sublocality_level_2" ]
        },
        {
           "long_name" : "Ramanathapuram",
           "short_name" : "Ramanathapuram",
           "types" : [ "political", "sublocality", "sublocality_level_1" ]
        },
        {
           "long_name" : "Coimbatore",
           "short_name" : "Coimbatore",
           "types" : [ "locality", "political" ]
        },
        {
           "long_name" : "Coimbatore",
           "short_name" : "Coimbatore",
           "types" : [ "administrative_area_level_2", "political" ]
        },
        {
           "long_name" : "Tamil Nadu",
           "short_name" : "TN",
           "types" : [ "administrative_area_level_1", "political" ]
        },
        {
           "long_name" : "India",
           "short_name" : "IN",
           "types" : [ "country", "political" ]
        },
        {
           "long_name" : "641045",
           "short_name" : "641045",
           "types" : [ "postal_code" ]
        }
     ],
     "formatted_address" : "Artsan Towers, 4, Trichy Rd, Near Ramanathapuram Signal, Behind SS Palamuthir Nilayam, Ramasamy Nagar, Ramanathapuram, Coimbatore, Tamil Nadu 641045, India",
     "geometry" : {
        "location" : {
           "lat" : 10.9974156,
           "lng" : 76.9949293
        },
        "location_type" : "ROOFTOP",
        "viewport" : {
           "northeast" : {
              "lat" : 10.9987645802915,
              "lng" : 76.99627828029151
           },
           "southwest" : {
              "lat" : 10.9960666197085,
              "lng" : 76.9935803197085
           }
        }
     },
     "partial_match" : true,
     "place_id" : "ChIJEVWhlsNZqDsR4AxMyrJZQPM",
     "types" : [ "street_address" ]
  }   ],   "status" : "OK"}

当我尝试实例化类时,例如:

class Fridge:
    def __init__ (self, food, quantity):
        self.food=food
        self.quantity=quantity
    def UserEntry(self):
        if input=="milk":
            print("you got milk!")
        else:
            print ("What do you want?")
    def DisplayFridge(self):
        print("Fridge_item#1 :" , self.food, "Quantity:" , self.quantity)

并且只要我打开括号以实例化如下的类:

test= Fridge 

我看到了传递给类构造函数/初始化方法的参数。 (即食物和数量)。

考虑到这一点......我有点不知道为什么我没有得到任何输出。我也被要求输入等等。

2 个答案:

答案 0 :(得分:1)

你没有这样输入,你应该尝试:

class Fridge:
    def __init__ (self, food, quantity):
        self.food=food
        self.quantity=quantity
    def UserEntry(self):
        var = raw_input("Please enter something: ")
        if var=="milk":
            print("you got milk!")
        else:
            print ("What do you want?")
    def DisplayFridge(self):
        print("Fridge_item#1 :" , self.food, "Quantity:" , self.quantity)

但是你的代码中缺乏逻辑:

  • 为什么从不使用UserEntry?
  • 你如何使用冰箱?
  • 您的userEntry方法永远不会更改您的self.food变量。

答案 1 :(得分:1)

如果您正在制作实例,请键入

test = Fridge(

然后它不会向您显示“传递给类构造函数/初始化方法的参数”,但它会向您显示为了创建实例而必须传递的内容。 / p>

E.g。

test = Fridge("milk", 10)

现在它持有10个奶。尝试

test.UserEntry()
test.DisplayFridge()