2

I'd like to make a program in Python 3 that would be essentially vocabulary flashcards. I'd be able to list the terms, add a term, or display a random definition to try and accurately guess. Once guessed accurately, I would be given the option for another definition to guess. Alternatively, I'd like to just be able to display a random key:value pair, and to continue viewing pairs until I enter EXIT.

I have made most of the program using a dictionary, but am not sure how to go about with entering the proper command to enter the key for the definition displayed. If anyone could provide suggestions, I'd appreciate it! Also I got some sort of error message when entering this code in and had to do a bunch of indentations, not sure what I did wrong there.

import random

terms = {"1" : "def 1", #Dictionary of 'terms' and 'definitions'
         "2" : "def 2",
         "3" : "def 3"}

menu = None
while menu != "4":
    print("""

    DIGITAL FLASHCARDS!

    1 - List Terms
    2 - Add Term
    3 - Guess Random Definition
    4 - Exit

    """)
    menu = input("\t\t\tEnter Menu option: ")
    if menu == "1":  # List Terms
        print("\n")
        for term in terms:
            print("\t\t\t", term)
        input("\n\tPress 'Enter' to return to Main Menu.\n")

    elif menu == "2":  # Add Term
        term = input("\n\tEnter the new term: ").upper()
        if term not in terms:
            definition = input("\tWhat is the definition? ")
            terms[term] = definition
            print("\n\t" + term, "has been added.")
        else:
            print("\n\tThat term already exists!")
            input("\n\tPress 'Enter' to return to Main Menu.\n")

    elif menu == "3": # Guess Random Definition. Once correct, choose new random definition
        print("\n\t\t\tType 'Exit' to return to Menu\n")
        choice = random.choice(list(terms.values()))
        print("\n\t" + choice + "\n")

        guess = None
        while guess != "EXIT":
            guess = str(input("\tWhat is the term? ")).upper()
YLJ
  • 2,940
  • 2
  • 18
  • 29
Cory D
  • 77
  • 1
  • 8
  • Your indentation is inconsistent, most noticeably in the add term section – Nick is tired Jun 30 '17 at 17:55
  • I know you listed your question, but i am still having trouble figuring out what you need. Could you rephrase it for me? – gavsta707 Jun 30 '17 at 18:08
  • 1
    Hi Cory. To reproduce the spacing that you have in your text editor (which is crucial to debugging Python) the easiest way is to copy-and-paste into the question editing box, then highlight the pasted text and press ctrl-k. This will move every line 4-spaces forward, which is what you want. – juanpa.arrivillaga Jun 30 '17 at 18:09
  • @juanpa.arrivillaga: That doesn't work if the code is indented using a mixture of tabs and spaces (which was the case here). – martineau Jun 30 '17 at 18:20
  • 1
    @martineau aaah good Point. Cory: **don't mix tabs and spaces in Python**. Most people just stick with 4-spaces. – juanpa.arrivillaga Jun 30 '17 at 18:21

1 Answers1

5

display a random definition to try and accurately guess. Once guessed accurately, I would be given the option for another definition to guess

Use terms.items() to get key and value at the same time.

Define the process of generating a new definition question into a function generate_question() to avoid duplicity.

elif menu == "3": # Guess Random Definition. Once correct, choose new random definition
    print("\n\t\t\tType 'Exit' to return to Menu\n")
    def generate_question():
        term, definition = random.choice(list(terms.items()))
        print("\n\t" + definition + "\n")
        return term
    term = generate_question()
    guess = None
    while guess != "EXIT":
        guess = input("\tWhat is the term? ").upper()
        if guess == term:
            print("Correct!")
            if input("\tAnother definition?(y/n)").upper() in ["Y", "YES"]:
                term = generate_question()
            else:
                break

Alternatively, I'd like to just be able to display a random key:value pair, and to continue viewing pairs until I enter EXIT.

elif menu == "4": # Random display a term-definition pair.
    print("\n\t\t\tType 'Exit' to return to Menu\n")
    exit = None
    while exit != "EXIT":
        term, definition = random.choice(list(terms.items()))
        print(term + ":", definition)
        exit = input("").upper()  # Press enter to continue.

Remember to modify the beginning part:

while menu != "5":
    print("""

    DIGITAL FLASHCARDS!

    1 - List Terms
    2 - Add Term
    3 - Guess Random Definition
    4 - View term-definition pairs
    5 - Exit

    """)
YLJ
  • 2,940
  • 2
  • 18
  • 29
  • Thank you everyone for the help with entering code into SO, I will know what to do next time. Thank you frankyjuang for the answer, I will let you know how it goes in a bit! – Cory D Jun 30 '17 at 19:24
  • I would like to say that the code you provided does exactly what I was hoping, thank you very much! Though I'm sure it's apparent, but I'm admitting that I'm pretty fresh to Python. Being so, I have no idea how I would get the program to save the appended items in the dictionary; after the program closes, all of the added items go away. How do I achieve this? Thanks again! – Cory D Jul 02 '17 at 01:05
  • Google "save dict to file". You'll get [this](https://stackoverflow.com/questions/19201290/how-to-save-a-dictionary-to-a-file). In short, use python built-in module `pickle`. – YLJ Jul 02 '17 at 06:15