📉
Tutorials
  • Computer History
  • Function
    • Finance
      • Calculate
    • Manage Data
    • Date&Time
    • Strings and Character
  • Snippets
    • Web Application
      • Hugo
      • JavaScript
        • Stopwatch using JavaScript?
    • Note
    • Start Project
      • GitHub
      • GitLab
    • Python Programming
      • Strings and Character Data
      • List
      • Dictionaries
    • Data Science
      • Setting Option
      • Get Data
  • Link Center
    • Next Articles
    • Google
    • Excel VBA
    • Python
      • Notebook
    • WebApp
      • Vue.js
    • Finance
    • Project
      • Kids
        • Scratch
      • Finance
        • Plotly.js
        • Portfolio
      • Mini Lab
        • Systems Administration
        • Auto Adjust Image
      • Sending Emails
      • ECS
        • Knowledge Base
        • ระบบผู้เชี่ยวชาญ (Expert System)
        • Check product
        • Compare two SQL databases
      • e-Library
        • Knowledge base
        • การจัดหมวดหมู่ห้องสมุด
        • Temp
      • AppSheet
        • บัญชีรายรับรายจ่าย
      • Weather App
      • COVID-19
  • Tutorials
    • Data Science
      • Data Science IPython notebooks
    • UX & UI
      • 7 กฎการออกแบบ UI
    • Web Scraping
      • Scrape Wikipedia Articles
      • Quick Start
    • GUI
      • pysimple
        • Create a GUI
      • Tkinter
        • Python Tkinter Tutorial
      • PyQt
        • PyQt Tutorial
    • MachineLearning
      • การพัฒนา Chat Bot
      • AI ผู้ช่วยใหม่ในการทำ Customer Segmentation
      • Customer Segmentation
      • ตัดคำภาษาไทย ด้วย PyThaiNLP API
    • Excel & VBA
      • INDEX กับ MATCH
      • รวมสูตร Excel ปี 2020
      • How to Write Code in a Spreadsheet
    • Visualization
      • Bokeh
        • Part I: Getting Started
        • Data visualization
        • Plotting a Line Graph
        • Panel Document
        • Interactive Data Visualization
    • VueJS
      • VueJS - Quick Guide
    • Django
      • Customize the Django Admin
      • พัฒนาเว็บด้วย Django
    • Git
      • วิธีสร้าง SSH Key
      • Git คืออะไร
      • เริ่มต้นใช้งาน Git
      • การใช้งาน Git และ Github
      • รวม 10 คำสั่ง Git
      • GIT Push and Pull
    • Finance
      • Stock Analysis using Pandas (Series)
      • Building Investment AI for fintech
      • Resampling Time Series
      • Python for Finance (Series)
      • Stock Data Analysis (Second Edition)
      • Get Stock Data Using Python
      • Stock Price Trend Analysis
      • Calculate Stock Returns
      • Quantitative Trading
      • Backtrader for Backtesting
      • Binance Python API
      • Pine Script (TradingView)
      • Stocks Analysis with Pandas and Scikit-Learn
      • Yahoo Finance API
      • Sentiment Analysis
      • yfinance Library
      • Stock Data Analysis
      • YAHOO_FIN
      • Algorithmic Trading
    • JavaScript
      • Split a number
      • Callback Function
      • The Best JavaScript Examples
      • File and FileReader
      • JavaScript Tutorial
      • Build Reusable HTML Components
      • Developing JavaScript components
      • JavaScript - Quick Guide
      • JavaScript Style Guide()
      • Beginner's Handbook
      • Date Now
    • Frontend
      • HTML
        • File Path
      • Static Site Generators.
        • Creating a New Theme
    • Flask
      • Flask - Quick Guide
      • Flask Dashboards
        • Black Dashboard
        • Light Blue
        • Flask Dashboard Argon
      • Create Flask App
        • Creating First Application
        • Rendering Pages Using Jinja
      • Jinja Templates
        • Primer on Jinja Templating
        • Jinja Template Document
      • Learning Flask
        • Ep.1 Your first Flask app
        • Ep.2 Flask application structure
        • Ep.3 Serving HTML files
        • Ep.4 Serving static files
        • Ep.5 Jinja template inheritance
        • Ep.6 Jinja template design
        • Ep.7 Working with forms in Flask
        • Ep.8 Generating dynamic URLs in Flask
        • Ep.9 Working with JSON data
        • Ep.23 Deploying Flask to a VM
        • Ep.24 Flask and Docker
        • Ep. 25: uWSGI Introduction
        • Ep. 26 Flask before and after request
        • Ep. 27 uWSGI Decorators
        • Ep. 28 uWSGI Decorators
        • Ep. 29 Flask MethodView
        • Ep. 30 Application factory pattern
      • The Flask Mega-Tutorial
        • Chapter 2: Templates
      • Building Flask Apps
      • Practical Flask tutorial series
      • Compiling SCSS to CSS
      • Flask application structure
    • Database
      • READING FROM DATABASES
      • SQLite
        • Data Management
        • Fast subsets of large datasets
      • Pickle Module
        • How to Persist Objects
      • Python SQL Libraries
        • Create Python apps using SQL Server
    • Python
      • Python vs JavaScript
      • Python Pillow – Adjust Image
      • Python Library for Google Search
      • Python 3 - Quick Guide
      • Regular Expressions
        • Python Regular Expressions
        • Regular Expression (RegEx)
        • Validate ZIP Codes
        • Regular Expression Tutorial
      • Python Turtle
      • Python Beginner's Handbook
      • From Beginner to Pro
      • Standard Library
      • Datetime Tutorial
        • Manipulate Times, Dates, and Time Spans
      • Work With a PDF
      • geeksforgeeks.org
        • Python Tutorial
      • Class
      • Modules
        • Modules List
        • pickle Module
      • Working With Files
        • Open, Read, Append, and Other File Handling
        • File Manipulation
        • Reading & Writing to text files
      • Virtual Environments
        • Virtual Environments made easy
        • Virtual Environmen
        • A Primer
        • for Beginners
      • Functions
        • Function Guide
        • Inner Functions
      • Learning Python
        • Pt. 4 Python Strings
        • Pt. 3 Python Variables
      • Zip Function
      • Iterators
      • Try and Except
        • Exceptions: Introduction
        • Exceptions Handling
        • try and excep
        • Errors and Exceptions
        • Errors & Exceptions
      • Control Flow
      • Lambda Functions
        • Lambda Expression คืออะไร
        • map() Function
      • Date and Time
        • Python datetime()
        • Get Current Date and Time
        • datetime in Python
      • Awesome Python
      • Dictionary
        • Dictionary Comprehension
        • ALL ABOUT DICTIONARIES
        • DefaultDict Type for Handling Missing Keys
        • The Definitive Guide
        • Why Functions Modify Lists and Dictionaries
      • Python Structures
      • Variable & Data Types
      • List
        • Lists Explained
        • List Comprehensions
          • Python List Comprehension
          • List Comprehensions in 5-minutes
          • List Comprehension
        • Python List
      • String
        • Strings and Character Data
        • Splitting, Concatenating, and Joining Strings
      • String Formatting
        • Improved String Formatting Syntax
        • String Formatting Best Practices
        • Remove Space
        • Add Spaces
      • Important basic syntax
      • List all the packages
      • comment
    • Pandas
      • Tutorial (GeeksforGeeks)
      • 10 minutes to pandas
      • Options and settings
      • เริ่มต้น Set Up Kaggle.com
      • Pandas - Quick Guide
      • Cookbook
      • NumPy
        • NumPy Package for Scientific
      • IO tools (text, CSV, …)
      • pandas.concat
      • Excel & Google Sheets
        • A Guide to Excel
        • Quickstart to the Google Sheets
        • Python Excel Tutorial: The Definitive Guide
      • Working With Text Data
        • Quickstart
      • API Reference
      • Groupby
      • DateTime Methods
      • DataFrame
      • sort_values()
      • Pundit: Accessing Data in DataFrames
      • datatable
        • DataFrame: to_json()
        • pydatatable
      • Read and Write Files
      • Data Analysis with Pandas
      • Pandas and Python: Top 10
      • 10 minutes to pandas
      • Getting Started with Pandas in Python
    • Markdown
      • Create Responsive HTML Emails
      • Using Markup Languages with Hugo
    • AngularJS
      • Learn AngularJS
    • CSS
      • The CSS Handbook
      • Box Shadow
      • Image Center
      • The CSS Handbook
      • The CSS Handbook
      • Loading Animation
      • CSS Grid Layout
      • Background Image Size
      • Flexbox
  • Series
    • จาวาสคริปต์เบื้องต้น
      • 1: รู้จักกับจาวาสคริปต์
  • Articles
    • Visualization
      • Dash
        • Introducing Dash
    • Finance
      • PyPortfolioOpt
      • Best Libraries for Finance
      • Detection of price support
      • Portfolio Optimization
      • Python Packages For Finance
    • Django
      • เริ่มต้น Django RestFramework
    • General
      • Heroku คืออะไร
      • How to Crack Passwords
    • Notebook
      • IPython Documentation
      • Importing Notebooks
      • Google Colab for Data Analytics
      • Creating Interactive Dashboards
      • The Definitive Guide
      • A gallery of interesting Jupyter Notebooks
      • Advanced Jupyter Notebooks
      • Converting HTML to Notebook
    • Pandas
      • Pandas_UI
      • Pandas Style API
      • Difference Between two Dataframes
      • 19 Essential Snippets in Pandas
      • Time Series Analysis
      • Selecting Columns in a DataFrame
      • Cleaning Up Currency Data
      • Combine Multiple Excel Worksheets
      • Stylin’ with Pandas
      • Pythonic Data Cleaning
      • Make Excel Faster
      • Reading Excel (xlsx) Files
      • How to use iloc and loc for Indexing
      • The Easiest Data Cleaning Method
    • Python
      • pip install package
      • Automating your daily tasks
      • Convert Speech to Text
      • Tutorial, Project Ideas, and Tips
      • Image Handling and Processing
        • Image Processing Part I
        • Image Processing Part II
        • Image tutorial
        • Image Processing with Numpy
        • Converts PIL Image to Numpy Array
      • Convert Dictionary To JSON
      • JSON Dump
      • Speech-to-Text Model
      • Convert Text to Speech
      • Tips & Tricks
        • Fundamentals for Data Science
        • Best Python Code Examples
        • Top 50 Tips & Tricks
        • 11 Beginner Tips
        • 10 Tips & Tricks
      • Password hashing
      • psutil
      • Lambda Expressions
    • Web Scraping
      • Web Scraping using Python
      • Build a Web Scraper
      • Web Scraping for beginner
      • Beautiful Soup
      • Scrape Websites
      • Python Web Scraping
        • Web Scraping Part 1
        • Web Scraping Part 2
        • Web Scraping Part 3
        • Web Scraping Part 4
      • Web Scraper
    • Frontend
      • Book Online with GitBook
      • Progressive Web App คืออะไร
      • self-host a Hugo web app
  • Examples
    • Django
      • Build a Portfolio App
      • SchoolManagement
    • Flask
      • Flask Stock Visualizer
      • Flask by Example
      • Building Flask Apps
      • Flask 101
    • OpenCV
      • Build a Celebrity Look-Alike
      • Face Detection-OpenCV
    • Python
      • Make Game FLASH CARD
      • Sending emails using Google
      • ตรวจหาภาพซ้ำด้วย Perceptual hashing
        • Sending Emails in Python
      • Deck of Cards
      • Extract Wikipedia Data
      • Convert Python File to EXE
      • Business Machine Learning
      • python-business-analytics
      • Simple Blackjack Game
      • Python Turtle Clock
      • Countdown
      • 3D Animation : Moon Phases
      • Defragmentation Algorithm
      • PDF File
        • จัดการข้อความ และรูป จากไฟล์ PDF ด้วย PDFBox
      • Reading and Generating QR codes
      • Generating Password
        • generate one-time password (OTP)
        • Random Password Generator
        • Generating Strong Password
      • PyQt: Building Calculator
      • List Files in a Directory
      • [Project] qID – โปรแกรมแต่งรูปง่ายๆ เพื่อการอัพลงเว็บ
      • Python and Google Docs to Build Books
      • Tools for Record Linking
      • Create Responsive HTML Email
      • psutil()
      • Transfer Learning for Deep Learning
      • ดึงข้อมูลคุณภาพอากาศประเทศไทย
        • Image Classification
    • Web Scraper
      • Scrape Wikipedia Articles
        • Untitled
      • How Scrape Websites with Python 3
    • Finance
      • Algorithmic Trading for Beginners
      • Parse TradingView Stock
      • Creating a stock price database with MariaDB and python
      • Source Code
        • stocks-list
      • Visualizing with D3
      • Real Time Stock in Excel using Python
      • Create Stock Quote Module
      • The Magic Formula Lost Its Sparkle?
      • Stock Market Analysis
      • Stock Portfolio Analyses Part 1
      • Stock Portfolio Analyses Part 2
      • Build A Dashboard In Python
      • Stock Market Predictions with LSTM
      • Trading example
      • Algorithmic Trading Strategies
      • DOWNLOAD FUNDAMENTALS DATA
      • Algorithmic Trading
      • numfin
      • Financial Machine Learning
      • Algorithm To Predict Stock Direction
      • Interactive Brokers API Code
      • The (Artificially) Intelligent Investor
      • Create Auto-Updating Excel of Stock Market
      • Stock Market Predictions
      • Automate Your Stock Portfolio
      • create an analytics dashboard
      • Bitcoin Price Notifications
      • Portfolio Management
    • WebApp
      • CSS
        • The Best CSS Examples
      • JavaScript
        • Memory Game
      • School Clock
      • Frontend Tutorials & Example
      • Side Menu Bar with sub-menu
      • Create Simple CPU Monitor App
      • Vue.js building a converter app
      • jQuery
        • The Best jQuery Examples
      • Image Slideshow
      • Handle Timezones
      • Text to Speech with Javascript
      • Building Blog for Your Portfolio
      • Responsive Website Layout
      • Maths Homework Generator
  • Books
    • Finance
      • Python for Finance (O'Reilly)
    • Website
      • Hugo
        • Go Bootcamp
        • Hugo in Action.
          • About this MEAP
          • Welcome
          • 1. The JAM stack with Hugo
          • 2. Live in 30 minutes
          • 3. Using Markup for content
          • 4. Content Management with Hugo
          • 5. Custom Pages and Customized Content
          • 6. Structuring web pages
          • A Appendix A.
          • B Appendix B.
          • C Appendix C.
    • Python
      • ภาษาไพธอนเบื้องต้น
      • Python Cheatsheet
        • Python Cheatsheet
      • Beginning Python
      • IPython Cookbook
      • The Quick Python Book
        • Case study
        • Part 1. Starting out
          • 1. About Python
          • 2. Getting started
          • 3. The Quick Python overview
        • Part 2. The essentials
          • 14. Exceptions
          • 13. Reading and writing files
          • 12. Using the filesystem
          • 11. Python programs
          • 10. Modules and scoping rules
          • 9. Functions
          • 8. Control flow
          • 4. The absolute basics
          • 5. Lists, tuples, and sets
          • 6. Strings
          • 7. Dictionaries
        • Part 3. Advanced language features
          • 19. Using Python libraries
          • 18. Packages
          • 17. Data types as objects
          • 16. Regular expressions
          • 15. Classes and OOP
        • Part 4. Working with data
          • Appendix B. Exercise answers
          • Appendix A. Python’s documentation
          • 24. Exploring data
          • 23. Saving data
          • 20. Basic file wrangling
          • 21. Processing data files
          • 22. Data over the network
      • The Hitchhiker’s Guide to Python
      • A Whirlwind Tour of Python
        • 9. Defining Functions
      • Automate the Boring Stuff
        • 4. Lists
        • 5. Dictionaries
        • 12. Web Scraping
        • 13. Excel
        • 14. Google Sheets
        • 15. PDF and Word
        • 16. CSV and JSON
    • IPython
    • Pandas
      • จัดการข้อมูลด้วย pandas เบื้องต้น
      • Pandas Tutorial
  • Link Center
    • Temp
  • เทควันโด
    • รวมเทคนิค
    • Help and Documentation
  • Image
    • Logistics
Powered by GitBook
On this page
  • FLASH CARD TESTER
  • GETTING STARTED
  • OPEN THE DIRECTORY IN ATOM
  • MAKE A PLAN
  • SETTING UP CARD DATA
  • READ A FILE
  • FOR LOOP ITERATOR
  • A FEW MORE QUESTIONS
  • GETTING CLI INPUT
  • CHECK FOR CORRECT ANSWER
  • ADDING THE CORRECT ANSWER WHEN INCORRECT
  • CASE SENSITIVITY
  • BRAINSTORM NEW FEATURES
  • KEEPING TRACK OF SCORE
  • Stretch Challenge: f-strings
  • IN REVIEW: YOU CAN ...

Was this helpful?

  1. Examples
  2. Python

Make Game FLASH CARD

FLASH CARD TESTER

Ever need to memorize something? Flash cards can really do the trick! So we're going to build a command line flash card tester application. Let's get started.

Imagine you want to learn the capitals of countries in the Middle East. Let's make some flashcards.

By the end of this chapter you will be able to:

  1. Read a file with python3

  2. Parse data from a JSON format into a python dictionary

  3. Iterate over an array

  4. Take user input from the command line

MARK AS COMPLETE

GETTING STARTED

Flash Cards will be made out of just one python file. You can tell if a file is python code if it ends in the .py document type.

If you don't already have one, navigate to the root of your computer, to the ~ directory, and make a new directory called code, then in that directory make a new directory called cli-games. We'll put all the games we make in this tutorial in this folder. In that directory make a new directory called flash-cards and in that directory make two files, one called flashcards.py and one called me-capitals.json (where we will put our flash card data of "Middle Eastern Capitals").

Make your initial project folder and two main files:

$ cd ~
$ mkdir code
$ cd code
$ mkdir cli-games
$ cd cli-games
$ mkdir flash-cards
$ cd flash-cards
$ touch flashcards.py
$ touch me-capitals.json

Now your folder should look like this:

| cli-games
  | flash-cards
    |- flashcards.py
    |- me-capitals.json
  ...

It is important to keep your coding projects organized on your computer. Having a centralized folder called code or dev where you put all projects is a great pattern to follow.MARK AS COMPLETE

OPEN THE DIRECTORY IN ATOM

You can use Atom like any other word processor you are used to, but you can also use it from the command line to open a single file or a whole project folder.

Let's try using the command line command atom . to open the directory we are currently in. Make sure you have navigated to the flash-cards directory, then type the following:

(flash-cards)$ atom .

Clarification - The period (.) in atom . means "this directory". You can also open a project using a path, e.g. from the root ~ type the command $ atom code/cli-gamesMARK AS COMPLETE

MAKE A PLAN

Software engineers always start any work by writing up a plan for how they will work on a project. This tutorial will provide you with this plan to start off with:

We have basically five steps:

  1. Setup the cards data that we want to quiz ourselves with

  2. Write code that reads the cards data and parses it into a python dictionary

  3. Write code that iterates over the cards

  4. Get the users's input for each question

  5. Check the users input against the answer

  6. Display "Correct!" or "Incorrect!"

MARK AS COMPLETE

SETTING UP CARD DATA

Let's imagine we have a list of Middle Eastern countries and their capitals. Let's write those into a format called JSON.

Go into your me-capitals.json file and let's add a little bit of JSON.

{
  "cards": [
    {
      "q": "What is the capital of Syria?",
      "a": "Damascus"
    }
  ]
}

This bit of JSON has one root key called cards, and the value associated with cards is an array of cards. Each element in the array [] is an object {} with two keys q for "question" and a for "answer".

JSON is a very popular way to store and transmit data in the internet. It is a great format for data because it is easy for computers and humans to read it and write it. JSON's basic structure is comma-delimited Key-Value Pairs surrounded by curly-bracies {}. Notice that keys are strings, and values can be any form of data, including arrays [], and objects {}. By having arrays and objects as values gives JSON a nested tree structure. As you use JSON more you will become very familiar with its structure.MARK AS COMPLETE

READ A FILE

A python script .py can read files around it using file system commands native to python.

Let's use these commands to read the me-capitals.json file and parse the JSON into a data format python can manipulate: a Dictionary. (Python can't work with JSON itself, it has to change it into an equivalent structure that it understands).

Now we need to read this file and print its contents.

# flashcards.py

# import the json module from python3
import json

# open the file and parse the JSON
with open('me-capitals.json', 'r') as f:
    data = json.load(f)
    print(data)

First we import the json module and then we use the use the open built-in function, and pass it two arguments:

  1. The name of the file: me-capitals.json

  2. An option—in this case 'r', which just means "read"

The as f: part means that the contents of the file is passed into the open function as the variable f.

So finally we use the json module we imported above to .load(f) that is to load the contents of the file and parse it from json into a python dictionary called data.

Always test your code after each step by running it and checking that the step you just completed is working. Run your project with this command:

$ python3 flashcards.py

When you run this, you should see the contents of the file:

{'cards': [{'q': 'What is the capital of Syria?', 'a': 'Damascus'}]}

The only difference between JSON and a python dictionary is the keys have '' single quotes whereas the JSON has "" double-quotes. Now that you are an engineer, you should be aware and focus on tiny differences like that because they matter to the computer.MARK AS COMPLETE

FOR LOOP ITERATOR

We've completed the first two steps, so next we have to iterate over the cards array:

  1. DONE - Setup the data

  2. DONE - Write code that reads the data file and parses it into a python dictionary

  3. Write code that iterates over the data

  4. Get the users's input for each question

  5. Check the users input against the answer and display "Correct!" or "Incorrect!"

Iterating over something in code is like a teacher calling roll. The teacher calls out a name in their list of students. Each student responds present if they are present, and there is no response if they are absent. In code, you could say the teacher iterates through the array of students and calls a function that returns “present” if the student is present, otherwise it returns "<>".

We will use what is called a For Loop iterator to iterate over all the cards. The For Loop has this structure:

for <<ITEM>> in <<LIST or ARRAY>>

Let's add a for loop to our flashcards:

# flashcards.py

# import the json module from python3
import json

# open the file and parse the JSON
with open('me-capitals.json', 'r') as f:
    data = json.load(f)

for i in data["cards"]:
    print(i)

data["cards"] is the way to access a value in a key-value pair inside a python dictionary. Since the structure looks like this:

{'cards': [{'q': 'What is the capital of Syria', 'a': 'Damascus'}]}

We need to access the cards key, to get back its array value [{'q': 'What is the capital of Syria', 'a': 'Damascus'}].

Run the program again using python3 flashcards.py and make sure you get output as described aboveMARK AS COMPLETE

A FEW MORE QUESTIONS

We're using learning the capitals of countries in the middle east for our cards.

Let's add a few more capitals to our me-capitals.json file:

{
  "cards": [
    {
      "q": "What is the capital of Syria?",
      "a": "Damascus"
    },
    {
      "q": "What is the capital of Lebanon?",
      "a": "Beirut"
    },
    {
      "q": "What is the capital of Israel?",
      "a": "Jerusalem"
    },
    {
      "q": "What is the capital of Jordan?",
      "a": "Amman"
    },
    {
      "q": "What is the capital of Iraq?",
      "a": "Baghdad"
    }
  ]
}

Now rerun the program and see if your iterator prints out each questionMARK AS COMPLETE

GETTING CLI INPUT

Ok now we need to get the user to actually try to answer each question. To access the question in each object, we'll do so using the same way we accessed cards inside of data: i["q"]. And we'll use the input built-in function to prompt the user to enter an input.

Update flashcards.py to the following:

# flashcards.py

# import the json module from python3
import json

# open the file and parse the JSON
with open('me-capitals.json', 'r') as f:
    data = json.load(f)

for i in data["cards"]:
    guess = input(i["q"] + " > ")
    print(guess)

Run this program and see that you can see your guess printed out.MARK AS COMPLETE

CHECK FOR CORRECT ANSWER

So now we've done four of our five steps we set out to do, so all we have now is to check if the answer is right:

  1. DONE - Setup the data

  2. DONE - Write code that reads the data file and parses it into a python dictionary

  3. DONE - Write code that iterates over the data

  4. DONE - Get the users's input for each question

  5. Check the users input against the answer and display "Correct!" or "Incorrect!"

We'll check correct and incorrect using the equality operator: ==.

Update your for loop in flashcards.py to the following:

# flashcards.py

import json

with open('me-capitals.json', 'r') as f:
    data = json.load(f)

for i in data["cards"]:
    guess = input(i["q"] + " > ")

    if guess == i["a"]:
        print("Correct!")
    else:
        print("Incorrect!")

A note on Clear Variable Naming. Variables can be named almost anything, so it is important to pick names that are logical and semantic. We named the user's input "guess" because in terms of this program, that is what it is, the user's input is a guess at what the flashcard's answer is. Always consider carefully what to name a variable and pick a logical and clear name.MARK AS COMPLETE

ADDING THE CORRECT ANSWER WHEN INCORRECT

Now let's polish up the user experience by display the correct answer when someone get's the answer wrong.

# flashcards.py

import json

with open('me-capitals.json', 'r') as f:
    data = json.load(f)

for i in data["cards"]:
    guess = input(i["q"] + " > ")

    if guess == i["a"]:
        print("Correct!")
    else:
        print("Incorrect! The correct answer was", i["a"])

Run the program and see how this looks. Pretty good?MARK AS COMPLETE

CASE SENSITIVITY

Notice right now that if you enter "jerusalem" instead of "Jerusalem" you get the answer wrong. We could remove this, but capitals are capitalized aren't they.

But what if we want to make other cards for biology or French words? Capitalizing the answer exactly will be annoying.

To fix this we can use the string.lower() string function to lowercase the whole word before we compare them.

...

if guess.lower() == i["a"].lower():

...

Now the comparison is only between all lowercase text no matter what. You can try this out by changing the code in flashcards.py if you want to see it in action.MARK AS COMPLETE

BRAINSTORM NEW FEATURES

What else could you add to this program? Here are some ideas:

  1. As them if they want to play again at the end.

  2. Randomize the order of questions.

  3. Keep playing until the player gets all the questions right at least once.

  4. Keep playing until the player answers correctly 10 in a row.

  5. Create various data files that are different sets of questions and let people pick which one they want to do.

  6. Let people enter their own cards and save those as libraries of questions and answers.

As a stretch challenge now or later, come back and try to implement one of these. But let's do one of them right now.MARK AS COMPLETE

KEEPING TRACK OF SCORE

Let's use a variable called score to keep track of score with each answer.

Update flashcards.py to the following:

# flashcards.py
import json

with open('me-capitals.json', 'r') as f:
    data = json.load(f)

# initialize total as the length of the cards array
total = len(data["cards"])
# initialize score as 0
score = 0

for i in data["cards"]:
    guess = input(i["q"] + " > ")

    if guess == i["a"]:
        # increment score up one
        score += 1
        # interpolate score and total into the response
        print(f"Correct! Current score: {score}/{total}")
    else:
        print("Incorrect! The correct answer was", i["a"])
        print(f"Current score: {score}/{total}")

Examine the code above and look at the comments. You can see that we have made 4 changes:

  1. We initialized total as the length of the cards array using the len(array) built-in function.

  2. We initialized score as 0 because everyone starts with 0 correct answers.

  3. Next when they get a right answer we increment score by 1 using += 1

  4. Finally we use a new feature of Python 3.6 called f-strings to interpolate {score} and {total} to our response in the correct and incorrect messages.

Check and see if yours works and troubleshoot any errors.

Stretch Challenge: f-strings

Can you use f-strings to change how we do string interpolations throughout this program?MARK AS COMPLETE

IN REVIEW: YOU CAN ...

So in review here's what you did and what you can do now:

  1. You can read a file with python3

  2. You can parse data from a JSON format into a python dictionary

  3. You can access elements in a python dictionary

  4. You can iterate over an array

  5. You can take user input from the command line

PreviousPythonNextSending emails using Google

Last updated 4 years ago

Was this helpful?

Now let's get started coding. To code we will use the code editor Atom. If you don't already have this code editor, head over to the and download and install it.

Reference :

Atom website
https://www.makeschool.com/academy/track/standalone/make-games-with-python-soy/flash-cards