Amino acid quiz using Python

I keep forgetting amino acid abbreviations and their classes because I never bothered learning them. To see if I can teach an old dog (me) new tricks, I wrote this simple Python script that asks questions about amino acids. I'll keep doing the quiz until I can finally remember.

Below is the Python code for an amino acid quiz.

#!/usr/bin/env python3

import random
import signal

my_list = {
    "name" : ["Alanine", "Arginine", "Asparagine", "Aspartate", "Cysteine", "Glutamine", "Glutamate", "Glycine", "Histidine", "Isoleucine", "Leucine", "Lysine", "Methionine", "Phenylalanine", "Proline", "Serine", "Threonine", "Tryptophan", "Tyrosine", "Valine"],
    "three letter symbol" : ["Ala", "Arg", "Asn", "Asp", "Cys", "Gln", "Glu", "Gly", "His", "Ile", "Leu", "Lys", "Met", "Phe", "Pro", "Ser", "Thr", "Trp", "Tyr", "Val"],
    "one letter symbol" : ["A", "R", "N", "D", "C", "Q", "E", "G", "H", "I", "L", "K", "M", "F", "P", "S", "T", "W", "Y", "V"],
    "class" : ["Aliphatic", "Fixed cation", "Amide", "Anion", "Thiol", "Amide", "Anion", "Aliphatic", "Aromatic cation", "Aliphatic", "Aliphatic", "Cation", "Thioether", "Aromatic", "Cyclic", "Hydroxylic", "Hydroxylic", "Aromatic", "Aromatic", "Aliphatic"]
}

def print_table():
    header = list(my_list.keys())
    print("\t".join(header))
    for i in range(len(my_list[header[0]])):
        line = [my_list.get(key)[i] for key in header]
        print("\t".join(line))

def ask_question(n):
    question_type = random.choice((list(my_list)))
    for_type = random.choice((list(my_list)[:-1]))
    while question_type == for_type:
        for_type = random.choice((list(my_list)[:-1]))

    i = random.randrange(len(my_list[for_type]))
    specific_type = my_list[for_type][i]

    question = f"{n}. What is the {question_type} for {specific_type}? "
    answer = my_list[question_type][i]
    attempt = input(question).lower()

    while attempt != answer.lower():
        if attempt == "answer":
            print(answer)
        elif attempt == "hint" or attempt == "help":
            print(set(my_list[question_type]))
        elif attempt == "table":
            print_table()
        else:
            print("Wrong!")
        attempt = input(question).lower()

    print("Correct!")
    if n == 10:
        again = input("Do you want to answer another 10 questions? [Y/n] ")
        if again == "n":
            print("Bye!")
            quit()
        else:
            ask_question(1)
    n += 1
    ask_question(n)

def handler(signum, frame):
    print("\nBye!")
    quit()

signal.signal(signal.SIGINT, handler)

print("Answer ten questions correctly!")
ask_question(1)

Run the script.

./aa.py
Answer ten questions correctly!
1. What is the one letter symbol for Alanine? A
Correct!
2. What is the one letter symbol for Leu? L
Correct!
3. What is the class for Alanine? amide
Wrong!
3. What is the class for Alanine?

You will have an option to quit after answering ten questions correctly (or if you press control+c).

If you get stuck, type "hint" or "help", which will output the possible answers. If you get really stuck, type "answer" and the answer will be displayed. You can also type "table" to display the amino acid table, which also gives you the answer.

Have fun!

Print Friendly, PDF & Email



Creative Commons License
This work is licensed under a Creative Commons
Attribution 4.0 International License
.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.