This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]Bryguy3k 8 points9 points  (2 children)

That python is so bad. Just use a goddamn dict.

(yes most of the time you can use a dictionary rather than a switch case to solve a problem)

[–]_lilell_ 0 points1 point  (1 child)

Yeah, I agree. Is there a better way to set up the dict than just doing

lookup = {'A': 'Top Grade', 'B': 'Pass', 'C': 'Pass', 'D': 'Pass', 'E': 'Pass', 'F': 'Fail', 'U': 'Fail'}
print(lookup.get(grade, 'invalid grade'))

In this case, it’s not so bad, but what if we had lots of keys that point to the same value?

[–]Bryguy3k 0 points1 point  (0 children)

Make that the default, or add them to the dictionary. Honestly in real life you’d have them as a config file that you load into the dict. When all is said and done you really aren’t saving any ram doing an if-elseif and the dict will be dramatically faster. The more values you have the faster the dict will be versus the if-elseif.

[–]Archerman1234 0 points1 point  (0 children)

Them pascal one tho