Code Monkey home page Code Monkey logo

jeopardy-cli-python-apis's Introduction

Build a Command Line Quiz Game!

Run on Repl.it

The Goal

We're going to build a CLI trivia game! When it's finished, it will look something like this:

Open on GitHub to see a preview of the Jeopardy game in action

We will use a url (those things we usually call 'links') to get random trivia questions!

Then, we're going to write a program to ask those questions to a user and tell them whether or not they got a question right.

Click for more detail

We've spent all day learning about data structures, and we've come up with a lot of ways to use them to make our code more efficient, but dictionaries and lists are hugely important because almost all data sent back and forth through the internet is sent packaged and organized in a structure that very closely matches a dictionary, though it is often referred to by other names.

Web applications typically relay this information through something called an application program interface - an API for short.


Note that this is a STRETCH lab, which means that most students will not start it at all, and those who do are only expected to get about halfway through level 1 in the time allotted.

Environment Setup

In order for this game to work, we will need a few things:

  1. A program that will "get" the quiz question. In other words, if we think of the url (the 'link') as an address, we need a way to go get the data waiting for us at that address. We will use a python library called "Requests."
  2. A way to easily read the information that comes back to us from that address.

Install a URL handler

This is pre-installed on Replit. If you're using Replit, skip this step.

To install Requests, type the following into the console:

pip3 install requests
If that worked, great! Skip this next step. If it failed, click here to get an alternative installation method:
First install pip:
curl https://bootstrap.pypa.io/get-pip.py | python3

Then use pip to install requests:
sudo pip3 install requests

We also need to tell the python program to actually USE the library we just installed. That's why line 1 of 'jeopardy.py' looks like this:

import requests

Enable a JSON formatter

We're going to use the Requests library to retrieve information from this address: http://jservice.io/api/clues?category=139

Go ahead and click on it.

It looks pretty messy and hard to read, doesn't it?

Click to see a more readable version:

JSONview Example

It looks like a mashup of lists and dictionaries!


This data structure is called a JSON object, and yes, it looks like a bunch of lists and dictionaries because that's exactly what it is! Once it's formatted, it's MUCH less intimidating.

To make YOUR browser format it this way, you'll want to find a plugin that will do this. There are many options; the example used one for Chrome called JSONview. To install that one, just search for "JSONview" and navigate to the Chrome Web Store.

JSONview Install

Note: While it is possible to do this without JSONview or another JSON formatter, it's extremely difficult.

Once you've installed Requests and enabled a JSON formatter, you're ready to jump in.

API Basics

We're going to use an api called jservice, and if you go to their homepage, you'll see some advice for how to use their API.

For example, if you want to see the first 10 categories you could choose from, you could use the url http://jservice.io/api/categories?count=10

Based on that url, you can probably already figure out how to show the first 50 categories too. Try it out!

While that's cool, the jService team also built a cool category interface that is a bit easier for humans to use: jService Popular Categories.

Earlier, you used http://jservice.io/api/clues?category=139 to get some questions. You'll notice that the category in the url is coded number 139; if you open the link, you'll see that all the questions are in the category "5-letter words".

Feel free to change the category to whatever other category you choose. When you've picked a category you like (or if you want to stick to 5-letter words), it's time to really get started.

The Challenges

run the code as it is:

python3 jeopardy.py

You'll notice that it prints the entire response out all at once. That's way too much. We'll go one step at a time and try to build this up as a fully functional and interactive game.

LEVEL 1

  1. Print out just the first item in the entire response.

  2. Print out just the question of the first item in the entire response.

You might notice that each of the dictionary keys has the letter u in front of it - this just means the string that follows is represented in Unicode, and it won't affect your code in any way today. You can igore the u.

  1. Refactor your code so that it prints out a random question, not just the first one every time.

  2. Now, get some user input after the question. If what they type matches the answer, print a "congratulations!" message of some sort. If it doesn't match, print a "sorry" message of some sort.

    • It's important to think about how specific Python's matching will be. If the user types "gorge" but the correct answer is "Gorge.", then the user will get the question wrong. Consider finding a way of making your program responsive to small variations in capitalization or punctuation.
  3. If you get it wrong, you probably still want to know the right answer - it can be so frustrating if you were sure you were right. If the user gets it wrong, print out a message that tells them what the correct answer really was.

LEVEL 2

  1. Wrap this game in a loop so the user can play multiple rounds.

  2. Create a score variable:

    • If the user gets a question right, increase their score by the point value of the question.
    • If the user gets a question wrong, decrease their score by the point value of the question.
    • Print out their score after each round.

LEVEL 3

  1. One of the most frustrating parts of this game is missing an answer due to typos, spelling errors, capitalization mismatches, or unexpected punctuation. Some of this is relatively easy to fix with Python's core methods, but there is no core function to show that "george" and "gorge" are SO CLOSE that the user probably actually knew the answer.
    • There's a library called similar-text that will let us examine how similar two strings are. Remember that you'll need to install it, just like we did for requests. You'll want to look it up, but it might look something like pip3 install similar_text
    • Install that library, import it at the top of your program, and then look at the documentation to figure out how to use it.
    • If the user's answer is really close to the real answer, let them know that they almost have it, but that they may want to type it more carefully.
    • It may also be beneficial to consider a way of recognizing that "Grapes of Wrath" and "The Grapes of Wrath" would be considered a mismatch. Are you going to give the user another chance if all they're missing is a small word like "the"?

Jeopardy Board

LEVEL 4

  1. The real jeopardy board has six categories, and 5 questions per category of increasing difficulty. That's 30 unique questions. The player also gets to CHOOSE which questions to answer and when.
    • Find a way to load all 30 questions, and then offer the user a choice of which question to attempt.
    • Find a way to print out a visual representation of the board in the console.
      • Bonus: print the score at the top or bottom of the board.
    • Keep track of which questions the user has already tried, and throw them an error if they try to access the same question twice.

jeopardy-cli-python-apis's People

Contributors

dfenjves avatar jolson615 avatar lericho avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.