Skip to content

Instantly share code, notes, and snippets.

@SebDeclercq
Created July 18, 2019 11:05
Show Gist options
  • Select an option

  • Save SebDeclercq/fb6ab354e7d510282e089e176ee40b4e to your computer and use it in GitHub Desktop.

Select an option

Save SebDeclercq/fb6ab354e7d510282e089e176ee40b4e to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from collections import UserList\n",
"from dataclasses import dataclass, field\n",
"from typing import Any, Dict, List, Sequence\n",
"\n",
"@dataclass\n",
"class Question:\n",
" question: str\n",
" answer: str\n",
" choices: Dict[str, str] = field(default_factory=dict)\n",
"\n",
" \n",
" def check_choice(self, choice: str) -> bool:\n",
" return choice.upper() == self.answer.upper()\n",
" \n",
" def __repr__(self) -> str:\n",
" elements_to_print: Sequence[str] = (\n",
" self.question,\n",
" *[f'{letter.upper()} - {choice}'\n",
" for letter, choice\n",
" in self.choices.items()\n",
" ]\n",
" )\n",
" return '\\n'.join(elements_to_print)\n",
"\n",
"class QuestionsList(UserList):\n",
" def __init__(self, els: List[Any], *args: Any, **kwargs: Any) -> None:\n",
" for el in els:\n",
" self._raise_if_not_question(el)\n",
" super().__init__(els, *args, **kwargs)\n",
" \n",
" def append(self, el: Any) -> None:\n",
" self._raise_if_not_question(el)\n",
" super().append(el)\n",
"\n",
" def _raise_if_not_question(self, el: Any) -> None:\n",
" if not isinstance(el, Question):\n",
" raise ValueError(f'{el.__class__.__name__} not allowed in a QuestionList')\n",
" \n",
"@dataclass\n",
"class Game:\n",
" questions: QuestionsList\n",
" \n",
" def play(self) -> None:\n",
" for question in self.questions:\n",
" player_choice: str = self._ask(question)\n",
" if question.check_choice(player_choice):\n",
" if question != self.questions[-1]:\n",
" print('Correct ! Next question !')\n",
" else:\n",
" print('YEAHHHH you win !!!')\n",
" else:\n",
" print('Oops ! Unfortunately your adventure ends here :-(')\n",
" break\n",
" \n",
" def _ask(self, question: Question) -> None:\n",
" print(question)\n",
" return input('Your answer > ').rstrip().upper()"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"questions: QuestionsList = QuestionsList((\n",
" Question(\n",
" question='How many XXX in a YYY ?',\n",
" choices={'A': 1, 'B': 2, 'C': 3, 'D': 4},\n",
" answer='B'\n",
" ),\n",
" Question(\n",
" question='How many AAA in a BBB ?',\n",
" choices={'A': 1, 'B': 2, 'C': 3, 'D': 4},\n",
" answer='A'\n",
" )\n",
"))\n",
"game: Game = Game(questions)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"How many XXX in a YYY ?\n",
"A - 1\n",
"B - 2\n",
"C - 3\n",
"D - 4\n",
"Your answer > B\n",
"Correct ! Next question !\n",
"How many AAA in a BBB ?\n",
"A - 1\n",
"B - 2\n",
"C - 3\n",
"D - 4\n",
"Your answer > A\n",
"YEAHHHH you win !!!\n"
]
}
],
"source": [
"game.play()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment