New to python, for some reason the if statement isn't functioning

Would love help

Fixed it

Good job figuring it out yourself! For future reference, please avoid posting pictures of code. The likelihood that someone will help you increases significantly if you post the code as properly formatted text.

Additionally, here’s the idiomatic way to do this in Python:

if name in ["harry", "hermione", "ron"]:
    ...
3 Likes

Should’ve used a set instead of a list for all the O(1) goodness :wink:

2 Likes

Fun fact: that goodness is only of value if the set is constant and literal, and can be replaced with a frozenset. If you have a variable in there, Python has to build the inclusion list on the fly, which eats up all your savings - AND it’s faster to build a tuple or list than a set. But thanks to frozenset literals, the code provided is slightly faster. Not that it’s going to matter much; I would prefer name in [...] to name == "..." or name == "..." even if it were slower, because we’re talking a few nanoseconds compared to the ease of adding/removing entries without having weird bugs.

1 Like

Hello,

a more pythonic way would have been to do this IMHO as alreay answered below:

if name in ('harry', 'hermione', 'ron'):

or

teams = {
   'Gryffindor': {'harry', 'hermione', 'ron'},
   'Slytherin': {'draco', },
   'Who?': set(),
}

for team, members in teams.items():
   if name in members:
       print(team)
       break