Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Python TypeError

So i've got this bot that im working but, but i came across this TypeError. All i want to do is to ensure that the content for the icon `` variable is decided on how the weather (main()) is like. I made sure thatmain()``` returns an int so if i set up an if statement, it will work properly. So as i was testing this if condition, i ran to this TypeError and got no clue what it means nor what im suppose to do. Here's the code if you're wondering:

class Weather_Icon(main()):
    icon = '?❄'
    icons = {'sunny': '☀ ?', 'normal': '? ?', 'cold': '❄ ⛄ ?'}
    for key, val in icons.items():
        if main() <= 40:
            icon = key[3]
        elif 41 <= main() <= 75:
            icon = key[2]
        elif main >= 76:
            icon = key[3]

Any suggestions are greatly appreciated ?

1 Answer

Hi Ammar, I don't think I totally get what your trying to achieve, but here's what I think anyways.

class Weather_Icon():  # incorrect use of inheritance
    icon = '?❄'
    icons = {'sunny': '☀ ?', 'normal': '? ?', 'cold': '❄ ⛄ ?'}
    for key, val in icons.items():
        if main() <= 40:
            icon = val   # remove key[], replace with val
        elif 41 <= main() <= 75:
            icon = val  # remove key[], replace with val
        elif main() >= 76:  # missing ()
            icon = val  # remove key[], replace with val

I'm sure this code could be simplified much further and or written better, but I'm not entirely sure how you are using the code. It makes no sense to iterate over each item in the dictionary to set the icon.

class WeatherIcon():
    def __init__(self, value):
        icons = {'sunny': '☀ ?', 'normal': '? ?', 'cold': '❄ ⛄ ?'}
        if value > 76:
            self.icon = icons['sunny']
        elif value > 41:
            self.icon = icons['normal']
        else:
            self.icon = icons['cold']

Job well done my friend :), i actually was working on a twitter bot that tweets weather and datetime. it's just been along time i haven't practiced oop. Thank you :)