{
  "format": "tokbit",
  "version": 1,
  "name": "Shopping list without duplicates",
  "main": [
    {
      "id": "u1",
      "kind": "declare",
      "name": "items",
      "type": "list[str]",
      "init": "[]",
      "comment": "The shopping list: empty at first."
    },
    {
      "id": "u2",
      "kind": "declare",
      "name": "option",
      "type": "str",
      "init": "\"\""
    },
    {
      "id": "u3",
      "kind": "declare",
      "name": "item",
      "type": "str",
      "init": "\"\""
    },
    {
      "id": "u4",
      "kind": "while",
      "condition": "option != \"5\"",
      "body": [
        {
          "id": "u5",
          "kind": "output",
          "values": "\"\""
        },
        {
          "id": "u6",
          "kind": "output",
          "values": "\"1 Add\""
        },
        {
          "id": "u7",
          "kind": "output",
          "values": "\"2 Remove\""
        },
        {
          "id": "u8",
          "kind": "output",
          "values": "\"3 List\""
        },
        {
          "id": "u9",
          "kind": "output",
          "values": "\"4 Count\""
        },
        {
          "id": "u10",
          "kind": "output",
          "values": "\"5 Quit\""
        },
        {
          "id": "u11",
          "kind": "input",
          "target": "option",
          "prompt": "Choice:"
        },
        {
          "id": "u12",
          "kind": "if",
          "condition": "option == \"1\"",
          "then": [
            {
              "id": "u13",
              "kind": "input",
              "target": "item",
              "prompt": "What to add:"
            },
            {
              "id": "u14",
              "kind": "if",
              "condition": "item in items",
              "then": [
                {
                  "id": "u15",
                  "kind": "output",
                  "values": "\"Already in the list.\""
                }
              ],
              "else": [
                {
                  "id": "u16",
                  "kind": "call",
                  "call": "items.append(item)"
                },
                {
                  "id": "u17",
                  "kind": "output",
                  "values": "\"Added.\""
                }
              ],
              "comment": "The in operator tells whether the item is already in the list."
            }
          ],
          "else": [
            {
              "id": "u18",
              "kind": "if",
              "condition": "option == \"2\"",
              "then": [
                {
                  "id": "u19",
                  "kind": "input",
                  "target": "item",
                  "prompt": "What to remove:"
                },
                {
                  "id": "u20",
                  "kind": "if",
                  "condition": "item in items",
                  "then": [
                    {
                      "id": "u21",
                      "kind": "call",
                      "call": "items.remove(item)"
                    },
                    {
                      "id": "u22",
                      "kind": "output",
                      "values": "\"Removed.\""
                    }
                  ],
                  "else": [
                    {
                      "id": "u23",
                      "kind": "output",
                      "values": "\"The item is not in the list.\""
                    }
                  ],
                  "comment": "remove deletes the first match; without the in check it would stop with an error."
                }
              ],
              "else": [
                {
                  "id": "u24",
                  "kind": "if",
                  "condition": "option == \"3\"",
                  "then": [
                    {
                      "id": "u25",
                      "kind": "if",
                      "condition": "len(items) == 0",
                      "then": [
                        {
                          "id": "u26",
                          "kind": "output",
                          "values": "\"The list is empty.\""
                        }
                      ],
                      "else": [
                        {
                          "id": "u27",
                          "kind": "for",
                          "variable": "i",
                          "range": "len(items)",
                          "body": [
                            {
                              "id": "u28",
                              "kind": "output",
                              "values": "str(i + 1) + \".\", items[i]"
                            }
                          ],
                          "comment": "Numbered from 1, the index starts at 0."
                        }
                      ]
                    }
                  ],
                  "else": [
                    {
                      "id": "u29",
                      "kind": "if",
                      "condition": "option == \"4\"",
                      "then": [
                        {
                          "id": "u30",
                          "kind": "output",
                          "values": "\"Number of items:\", len(items)"
                        }
                      ],
                      "else": [
                        {
                          "id": "u31",
                          "kind": "if",
                          "condition": "option != \"5\"",
                          "then": [
                            {
                              "id": "u32",
                              "kind": "output",
                              "values": "\"Unknown choice.\""
                            }
                          ],
                          "else": []
                        }
                      ]
                    }
                  ]
                }
              ]
            }
          ]
        }
      ],
      "comment": "A menu in a loop until you choose Quit."
    }
  ],
  "functions": [],
  "comment": "Solution to the task “No duplicates”: An item already in the list is not added; the program says “Already in the list.”",
  "screen": "fit"
}
