Skip to content

Instantly share code, notes, and snippets.

@DaniSancas
Created March 25, 2022 17:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DaniSancas/41825bb4f158cc7dd838e2597682f402 to your computer and use it in GitHub Desktop.
Save DaniSancas/41825bb4f158cc7dd838e2597682f402 to your computer and use it in GitHub Desktop.
Random quiz (4 candidate answers) in Python3 to practice ZSH keybindings
import random
all_quizzes = {
"Ctrl+a": "Beginning of line",
"Ctrl+e": "End of line",
"Ctrl+f": "Forward one character",
"Ctrl+b": "Back one character",
"Ctrl+h": "Delete one character (backwards)",
"Alt+f": "Forward one word",
"Alt+b": "Back one word",
"Ctrl+w": "Delete one word (backwards)",
"Ctrl+u": "Clear to beginning of line",
"Ctrl+k": "Clear to end of line",
"Ctrl+p": "Previous line in history",
"Ctrl+n": "Next line in history",
"Ctrl+r": "Search backwards in history",
"Ctrl+l": "Clear screen",
"Ctrl+o": "Execute command but keep line",
"Ctrl+z": "Suspend process",
"Ctrl+c": "Kill current process",
}
def select_quiz_candidates(quiz: dict, n: int = 4) -> dict:
key_list = random.sample(list(quiz), n)
selected_quizzes = {k: quiz[k] for k in key_list}
return {
"quiz": selected_quizzes,
"key": random.choice(list(selected_quizzes)),
"ask_key": random.choice([True, False])
}
def ask_questionaire_until_ok(quiz: dict, key: str, ask_key: bool):
enumerated = {n: k for n, k in enumerate(list(quiz))}
correct = False
while not correct:
print(f"Question: {key if ask_key else quiz[key]}")
for n, k in enumerated.items():
print(f"[{n}]: {quiz[k] if ask_key else k}")
answer = int(input("Choose your answer: "))
if enumerated[answer] == key:
print("Congrats!")
correct = True
else:
print("Wrong answer!")
if __name__ == "__main__":
candidates = select_quiz_candidates(all_quizzes)
ask_questionaire_until_ok(**candidates)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment