简体   繁体   English

类型错误,我该如何解决这个错误,在图片中我写了我的具体问题

[英]Type Error, How can I solve this error, In the image I wrote my specific question

This is my first question.这是我的第一个问题。 I've solving my Python homework, Making the information of Korean Subway.我已经解决了我的 Python 作业,制作韩国地铁的信息。

enter image description here在此处输入图片说明

In the image, I wrote an question.在图片中,我写了一个问题。 How can I solve this Error?我该如何解决这个错误?

class SubwayLine:
    def __init__(self, stations):
        self.stations = stations
        
    def __str__(self):
        return str(stations)
    
    
keys = ['Line1', 'Line2', 'Line3', 'Line4']
values = subwayStation
subwayStation = {}
for line, stations in zip(keys, values):
    subwayStation[line] = SubwayLine(stations)
    
print(subwayStation['Line1'])

check your values variable you are passing subwayStations not real stations name like you have commented.检查您正在通过地铁站的values变量,而不是您评论的真实站名。 Next time paste code not image.下次粘贴代码而不是图像。 small suggestion choose variable name according to their nature like keys and values use in dict so avoid that and their will be one station so use station not stations in class.小建议根据它们的性质选择变量名称,例如 dict 中使用的键和值,因此避免这种情况,它们将是一个站,因此在课堂上使用站而不是站。

class SubwayLine:
    def __init__(self,stations):
        self.stations = stations

    def __str__(self):
        return str(stations)



keys = ['Line1']
values = ['Seoul']
subwayStation = {}

for line,stations in zip(keys,values):
    subwayStation[line] = SubwayLine(stations)

print(subwayStation['Line1'])

The TypeError is raised when an operation or function is applied to an object of inappropriate type.当操作或函数应用于不适当类型的对象时,会引发 TypeError。

Change the variable name of your dictionary it's conflicting with your values list.更改与您的值列表冲突的字典的变量名称。

Do not post pictures of code, please follow these guidelines while asking the question.不要发布代码图片,请在提问时遵循这些准则

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM