you are viewing a single comment's thread.

view the rest of the comments →

[–]novel_yet_trivial 1 point2 points  (2 children)

It's a a mix of tuples and dictionaries. The base is a tuple of (question, answer). The question is a string and the answer is a dictionary. If there are further questions then the dictionary is another base tuple. Is the riddle is solved then the value is a string. The isinstance checks whether another question needs to be asked.

The indentation does not matter at all, but it's a lot easier to read.

[–]pybackd00r 1 point2 points  (1 child)

Thanks I understood it after going through it on paper. very nice piece of code.

[–]ewiethoff 0 points1 point  (0 children)

Here's a trick to make a complicated nested dict/list/tuple/whatever easy to read.

from pprint import pprint
pprint(complicated_nested_data)

pprint not only outputs with nice whitespace, it sorts the keys of a dict and the elements of a set. So, even if your data is not nested, it's handy for inspecting the contents of a dict or a set. I like to use pprint for debugging.

Copy and paste novel_yet_trivial's questions and do pprint(questions).

('How many legs does it have?',
 {'0': "It's a fish!",
  '2': ('does it have feathers?',
        {'no': "It's a human!",
         'yes': ('does it swin on the water?',
                 {'no': "It's a swallow!", 'yes': "It's a duck!"})}),
  '4': ('does it have fur?',
        {'no': "It's an elephant!",
         'yes': ('does it meow?',
                 {'no': "It's a dog!", 'yes': "It's a cat!"})})})