starts quiz code
This commit is contained in:
parent
824acc8ab0
commit
a593aeaaeb
File diff suppressed because one or more lines are too long
|
@ -2287,9 +2287,45 @@ word: apple
|
|||
do you want to play a new game? (Y/N)
|
||||
```
|
||||
|
||||
When looking at the game above we can break down the game logic as follows.
|
||||
|
||||
1. A welcome message is printed
|
||||
2. A *word-to-find* is chosen randomly from a database
|
||||
3. The word is shown as `*****` to hint at the length of the word
|
||||
4. A prompt is presented to the player and the can input **one** letter
|
||||
5. If the letter is present in the *word-to-guess* it is saved and shown from now on
|
||||
6. The previous 2 steps are **repeated** until the full word is found
|
||||
7. A winning banner is shown
|
||||
8. The player is asked to play a new game or not.
|
||||
|
||||
## Trivial pursuit multiple choice game
|
||||
|
||||
TODO [db](https://opentdb.com/api_config.php)
|
||||
We can fetch questions for an online [api](https://opentdb.com/api_config.php) to make a game.
|
||||
I downloaded a mini set of [questions](./assets/quiz_data.json) to help you get started.
|
||||
Have a look at the code below to understand how to develop your game logic.
|
||||
Once you successfully build a game you can try and integrate the [requests](https://docs.python-requests.org/en/latest/) library to get fresh questions each time you play the game.
|
||||
|
||||
```python
|
||||
import json
|
||||
import html
|
||||
|
||||
with open("./assets/quiz_data.json", "r") as fp:
|
||||
data = json.load(fp)
|
||||
|
||||
for key, value in data.items():
|
||||
print(key)
|
||||
|
||||
for question in data["results"]:
|
||||
for key, value in question.items():
|
||||
print(key)
|
||||
|
||||
for question in data["results"]:
|
||||
print("{}".format(html.unescape(question["question"])))
|
||||
choices = list()
|
||||
choices.append(question["correct_answer"])
|
||||
choices.extend(question["incorrect_answers"])
|
||||
for choice in enumerate(choices):
|
||||
print(html.unescape("\t {} {}".format(*choice)))```
|
||||
|
||||
### Introduction to the `requests` library
|
||||
|
||||
|
|
Loading…
Reference in New Issue