How to build flashcard in Python

In this article, we will learn how to build flashcards in Python. A flashcard is a card having data on the two sides, which can be utilized as a guide in memorization. Flashcards generally have an inquiry on one side and an answer on the other.

We will build a flashcard for the state vs capital quiz. Where flashcard shows a question and we need to answer the answer.

Flash Card in Python

1. Import necessary modules

2. Create a class named flashcard.

3. Now, initialize dictionary capitals that store state as key and capital as value.

4. Now randomly choose a pair from capitals using the choice() method.

5. Now prompt the user to answer the capital of the randomly chosen state.

6. If correct print correct else print wrong.

import random 

class flashcard: 
  def __init__(self): 
    
    self.capitals={'Arunachal Pradesh':'Itanagar', 
          'Assam':'Dispur', 
          'Bihar':'Patna', 
          'Chhattisgarh':'Raipur',
          'Goa':'Panaji',
          'Gujarat':'Gandhinagar',
          'Haryana':'Chandigarh',
          'Himachal Pradesh':'Shimla',
          'Jharkhand':'Ranchi',
          'Kerala':'Thiruvananthapuram',
          'Madhya Pradesh':'Bhopal',
          'Maharashtra':'Mumbai',
          'Manipur':'Imphal',
          'Meghalaya':'Shillong',
          'Mizoram':'Aizawl',
          'Nagaland':'Kohima',
          'Odisha':'Bhubaneswar',
          'Punjab':'Chandigarh',
          'Rajasthan':'Jaipur',
          'Sikkim':'Gangtok',
          'Tamil Nadu':'Chennai',
          'Telangana':'Hyderabad',
          'Tripura':'Agartala',
          'Uttar Pradesh':'Lucknow',
          'West Bengal':'Kolkata'} 
    
  def quiz(self): 
    while (True): 
      
      state, capital = random.choice(list(self.captials.items())) 
      
      print(">What is the capital of {}".format(state)) 
      user_answer = input() 
      
      if(user_answer.lower() == capital.lower()): 
        print("Correct answer") 
      else: 
        print("Wrong answer") 
        
      option = int(input("enter 0 , if you want to play again : ")) 
      if (option): 
        break

print("Welcome to the state vs capital quiz ") 
fc=flashcard() 
fc.quiz()

Output

Welcome to the state vs capital quiz 
>What is the capital of Goa
Panaji
Correct answer
enter 0 , if you want to play again : 0
>What is the capital of Arunachal Pradesh
Itanagar
Correct answer
enter 0 , if you want to play again : 0
>What is the capital of Telangana
Hyderabad
Correct answer
enter 0 , if you want to play again : 0
>What is the capital of Meghalaya
Shillong
Correct answer
enter 0 , if you want to play again : 0
>What is the capital of Nagaland
Aizawl
Wrong answer
enter 0 , if you want to play again : 1

Leave a Reply

Your email address will not be published.