简体   繁体   中英

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.

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. 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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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