Open In App

Python program to build flashcard using class in Python

Last Updated : 20 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will see how to build a flashcard using class in python. A flashcard is a card having information on both sides, which can be used as an aid in memoization. Flashcards usually have a question on one side and an answer on the other. Particularly in this article, we are going to create flashcards that will be having a word and its meaning.

Let’s see some examples of flashcard:

Example 1:

Approach :

  • Take the word and its meaning as input from the user.
  • Create a class named flashcard, use the __init__() function to assign values for Word and Meaning.
  • Now we use the __str__() function to return a string that contains the word and meaning.
  • Store the returned strings in a list named flash.
  • Use a while loop to print all the stored flashcards.

Below is the full implementation:

Python3




class flashcard:
    def __init__(self, word, meaning):
        self.word = word
        self.meaning = meaning
    def __str__(self):
       
        #we will return a string
        return self.word+' ( '+self.meaning+' )'
       
flash = []
print("welcome to flashcard application")
 
#the following loop will be repeated until
#user stops to add the flashcards
while(True):
    word = input("enter the name you want to add to flashcard : ")
    meaning = input("enter the meaning of the word : ")
     
    flash.append(flashcard(word, meaning))
    option = int(input("enter 0 , if you want to add another flashcard : "))
     
    if(option):
        break
         
# printing all the flashcards
print("\nYour flashcards")
for i in flash:
    print(">", i)


Output:

Time Complexity : O(n)

Space Complexity : O(n)

Example 2:

Approach :

  • Create a class named flashcard.
  • Initialize dictionary fruits using __init__() method.
  • Now randomly choose a pair from fruits using choice() method and store the key in variable fruit and value in variable color.
  • Now prompt the user to answer the color of the randomly chosen fruit.
  • If correct print correct else print wrong.

Python3




import random
 
class flashcard:
    def __init__(self):
       
        self.fruits={'apple':'red',
                     'orange':'orange',
                     'watermelon':'green',
                     'banana':'yellow'}
         
    def quiz(self):
        while (True):
           
            fruit, color = random.choice(list(self.fruits.items()))
             
            print("What is the color of {}".format(fruit))
            user_answer = input()
             
            if(user_answer.lower() == color):
                print("Correct answer")
            else:
                print("Wrong answer")
                 
            option = int(input("enter 0 , if you want to play again : "))
            if (option):
                break
 
print("welcome to fruit quiz ")
fc=flashcard()
fc.quiz()


Output:

Time Complexity : O(1)

Space Complexity : O(1)



Previous Article
Next Article

Similar Reads

How to Build a Twitter Bot to Post Latest Stock Update using Python
The stock market is volatile and changes rapidly so we are going to create a simple Twitter bot to post the latest stock updates using Python that posts the tweet about the stocks that users have chosen. First, let's understand the prerequisites of our project: Nsepython: It is a Python library to get publicly available data on the current NSEIndia
5 min read
Python | Build a REST API using Flask
Prerequisite: Introduction to Rest API REST stands for REpresentational State Transfer and is an architectural style used in modern web development. It defines a set or rules/constraints for a web application to send and receive data. In this article, we will build a REST API in Python using the Flask framework. Flask is a popular micro framework f
3 min read
Build a simple Quantum Circuit using IBM Qiskit in Python
Qiskit is an open source framework for quantum computing. It provides tools for creating and manipulating quantum programs and running them on prototype quantum devices on IBM Q Experience or on simulators on a local computer. Let's see how we can create simple Quantum circuit and test it on a real Quantum computer or simulate in our computer local
5 min read
Build a Virtual Assistant Using Python
Virtual desktop assistant is an awesome thing. If you want your machine to run on your command like Jarvis did for Tony. Yes it is possible. It is possible using Python. Python offers a good major library so that we can use it for making a virtual assistant. Windows has Sapi5 and Linux has Espeak which can help us in having the voice from our machi
8 min read
Build a basic Text Editor using Tkinter in Python
Tkinter is a Python Package for creating GUI applications. Python has a lot of GUI frameworks, but this is the only framework that’s built into the Python standard library. It has several strengths; it’s cross-platform, so the same code works on Windows, macOS, and Linux. It is lightweight and relatively painless to use compared to other frameworks
4 min read
Build a COVID19 Vaccine Tracker Using Python
As we know the world is facing an unprecedented challenge with communities and economies everywhere affected by the COVID19. So, we are going to do some fun during this time by tracking their vaccine. Let's see a simple Python script to improve for tracking the COVID19 vaccine. Modules Neededbs4: Beautiful Soup(bs4) is a Python library for pulling
2 min read
Build Fuel Price Tracker Using Python
In this modern-day lifestyle, fuel has become a necessity for all human beings. It is a base for our life-style. So, we are going to write a script to track their price using Python. Modules Needed bs4: Beautiful Soup(bs4) is a Python library for pulling data out of HTML and XML files. This module does not come built-in with Python. To install this
3 min read
Build a GUI Application to Get Live Stock Price using Python
The stock price is the highest amount someone is willing to pay for the stock. In this article, we are going to write code for getting live share prices for each company and bind it with GUI Application. Module Needed Yahoo_fin: This module is used to scrape historical stock price data, as well as to provide current information on market caps, divi
3 min read
Build a Voice Recorder GUI using Python
Prerequisites: Python GUI – tkinter, Create a Voice Recorder using Python Python provides various tools and can be used for various purposes. One such purpose is recording voice. It can be done using the sounddevice module. This recorded file can be saved using the soundfile module Module NeededSounddevice: The sounddevice module provides bindings
2 min read
Build an Application to extract URL and Metadata from a PDF using Python
The PDF (Portable Document Format) is the most common use platform-independent file format developed by Adobe to present documents. There are lots of PDF-related packages for Python, one of them is the pdfx module. The pdfx module is used to extract URL, MetaData, and Plain text from a given PDF or PDF URL. Features: Extract references and metadata
3 min read
Practice Tags :