Created
December 29, 2016 12:47
-
-
Save alex-pat/7f7994cd84517ca3a4abbc3a3897b467 to your computer and use it in GitHub Desktop.
токс
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# Как юзать | |
# python3 vksis-testing.py файл_с_вопросами.txt | |
import sys | |
import os | |
from random import choice | |
def check_args(): | |
if len(sys.argv) != 2: | |
print("Usage: ./toks.py [test_name]", file=sys.stderr) | |
sys.exit(1) | |
def parse_file(): | |
tests = [] | |
lines = open(sys.argv[1]).readlines() | |
lines.reverse() | |
while lines[-1] == '\n': | |
lines.pop() | |
while lines: | |
test = { | |
'question': lines.pop()[:-1], | |
'answers': [], | |
} | |
answer = lines.pop()[:-1] | |
while answer.strip(): | |
test['answers'].append(answer) | |
if not lines: | |
break | |
answer = lines.pop()[:-1] | |
test['correct'] = ( | |
[num for num, answ in enumerate(test['answers'], 1) if answ[0] == '*']) | |
tests.append(test) | |
while lines and not lines[-1][:-1].strip(): | |
lines.pop() | |
return tests | |
def intro(tests): | |
os.system('clear') | |
print(""" | |
T O K S | |
Written by Alexander Pateenok, 2016 | |
From 450501 with love, | |
Make Session Great Again, все дела | |
{number} questions in {file} | |
Press Enter to start (write 'quit' to exit) | |
""".format(number=len(tests), file=sys.argv[1])) | |
input() | |
os.system('clear') | |
def print_question(question, correct, all): | |
print("TOKS: {corr}/{all} right answers\n".format( | |
corr=correct, | |
all=all | |
)) | |
print(question['question']) | |
for num, answ in enumerate(question['answers'], 1): | |
print(num, answ[1:]) | |
print() | |
def testing(tests): | |
intro(tests) | |
correct_answers = 0 | |
all_questions = 0 | |
answer = '' | |
while True: | |
question = choice(tests) | |
print_question(question, correct_answers, all_questions) | |
answer = input('Answer (separate by spaces): ') | |
if answer == 'quit': | |
return | |
answers = [int(i) for i in answer.split()] | |
answers.sort() | |
if answers == question['correct']: | |
print("Correct!") | |
correct_answers += 1 | |
else: | |
print("Nope, correct:", question['correct']) | |
all_questions += 1 | |
input() | |
os.system('clear') | |
if __name__ == '__main__': | |
check_args() | |
testing(parse_file()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment