如何在python中设置值并获取值/变量?

2024-06-16 11:04:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我对编码有点陌生

这是我的代码和我的错误

supplier_105 = {
    "city": "Port Bradley",
    "contact_firstname": "Brittany",
    "contact_lastname": "Costa",
    "contact_title": "Mrs",
    "country": "Australia",
    "email": "brittany8706.costa@gmail.com",
    "notes": "",
    "phone": "(08) 6939 8022",
    "postcode": "3880",
    "state": "Costa",
    "street_address": "6/81 Heather Rosebowl",
    "supplier_id": 105,
    "supplier_name": "Rodriguez, Carter and Johnson"
}

# Write your code here
# 1. Define the class here
class Supplier:
   
      def __init__(self, city, contact_firstname,contact_lastname,contact_title,country,email,notes,phone,postcode,state,street_address,supplier_id,supplier_name):
        print("Initialiser called")
        self.city = city
        self.contact_firstname = contact_firstname
        self.contact_lastname = contact_lastname 
        self.contact_title = contact_title
        self.country = country
        self.email = email
        self.notes= notes
        self.phone = phone
        self.postcode = postcode
        self.state = state
        self.street_address = street_address
        self.supplier_id = supplier_id
        self.supplier_name = supplier_name
        

def get_state(self):
    return "The state is {} and the postcode is {}".format(self.state, self.postcode)

print(Supplier_105.supplier_name)

# 2. Instantiate the class
Supplier_105 = Supplier("Port Bradley","Brittany","Costa","Mrs","Australia","brittany8706.costa@gmail.com","","(08) 6939 8022","3880","Costa","6/81 Heather Rosebowl","105","Rodriguez, Carter and Johnson")

# 3. Call its methods here
print(Supplier_105.get_state())

错误

NameError                                 Traceback (most recent call last)
<ipython-input-3-70798f9ef411> in <module>
     39         return "{} is {} years old".format(self.state, self.postcode)
     40 
---> 41 print(Supplier_105.supplier_name)
     42 
     43 # 2. Instantiate the class

NameError: name 'Supplier_105' is not defined

我正在尝试使用方法/函数将州和邮政编码格式化为“州和邮政编码”,但它不起作用。我还试图将实例变量中的邮政编码设置为传递的参数值

在此方面的任何帮助都将不胜感激<;三,


Tags: nameselfcitytitleemailcontactfirstnamecountry
2条回答

您好,如果我理解正确,您希望能够访问并更改supplier_105对象中的变量stoe。现在我们开始:

  1. 代码中的供应商_105设置为字典。字典与键/值系统一起工作。它们是不可编辑的,因为其中的对象没有任何索引如果不需要使用在代码顶部创建的词汇,请跳过步骤1和2。

  2. 也要访问词汇表中的变量,您必须检查要获取的值的正确键。在您的情况下,要访问city的价值,您必须填写:supplier_105[“city”]

  3. 缩进有问题如果您想设置类供应商的get和set,则应按如下方式缩进类供应商:

Blockquote

class Geek:
    def __init__(self, age = 0):
         self._age = age
    def get_age(self):
        return self._age
    def set_age(self, x):
        self._age = x
  1. 您得到的错误是返回您的Supplier_105。未定义Supplier_名称,因为您在打印后定义了类对象。因为它没有实例化为类对象,所以他正在查找您的字典(也称为Supplier_105)。我建议给类对象和字典起不同的名字,以避免混淆

祝你好运,记住罗马不是一天建成的。编程在你开始的时候是很困难的,但是如果你坚持下去,那将是非常有益的。祝你好运,如果你有任何问题,不要犹豫

几个错误

  • 类的对象是在使用后定义的
  • 方法get_state(state)缩进错误
supplier_105 = {
    "city": "Port Bradley",
    "contact_firstname": "Brittany",
    "contact_lastname": "Costa",
    "contact_title": "Mrs",
    "country": "Australia",
    "email": "brittany8706.costa@gmail.com",
    "notes": "",
    "phone": "(08) 6939 8022",
    "postcode": "3880",
    "state": "Costa",
    "street_address": "6/81 Heather Rosebowl",
    "supplier_id": 105,
    "supplier_name": "Rodriguez, Carter and Johnson",
}


# Write your code here
# 1. Define the class here
class Supplier:
    def __init__(
        self,
        city,
        contact_firstname,
        contact_lastname,
        contact_title,
        country,
        email,
        notes,
        phone,
        postcode,
        state,
        street_address,
        supplier_id,
        supplier_name,
    ):
        print("Initialiser called")
        self.city = city
        self.contact_firstname = contact_firstname
        self.contact_lastname = contact_lastname
        self.contact_title = contact_title
        self.country = country
        self.email = email
        self.notes = notes
        self.phone = phone
        self.postcode = postcode
        self.state = state
        self.street_address = street_address
        self.supplier_id = supplier_id
        self.supplier_name = supplier_name

    def get_state(self):
        return "The state is {} and the postcode is {}".format(self.state, self.postcode)


# 2. Instantiate the class
Supplier_105 = Supplier(
    "Port Bradley",
    "Brittany",
    "Costa",
    "Mrs",
    "Australia",
    "brittany8706.costa@gmail.com",
    "",
    "(08) 6939 8022",
    "3880",
    "Costa",
    "6/81 Heather Rosebowl",
    "105",
    "Rodriguez, Carter and Johnson",
)
print(Supplier_105.supplier_name)

# 3. Call its methods here
print(Supplier_105.get_state())

相关问题 更多 >