compare - Python quiz, need guidance -


this first code if bear me , thorough possible.

this quiz asks question , has 4 possible answers choose 1-4. file reads looks this. right answer displayed above question.

a sport uses term love ? a)tennis b)golf c)football d)swimming b german word water ? a)wodar b)wasser c)werkip d)waski 

what want grab chunk without answer question now, questions , possible answers. , when guess compare answer real answer see if matches, if you'll score , if doesn't don't. biggest queries how compare , these chunks without answer.

quiz_file = '/home/wryther/desktop/quiz.dat' num_questions = 146  def get_chunk(buf, n):      while buf:         chunk = [buf.readline() _ in range(n)]           if not any(chunk):              chunk = none           yield chunk  def get_awns(ans):  #this transfomrs ints answers str compare if right or wrong.    if ans == 1:          ans = 'a'     elif ans == 2:          ans = 'b'     elif ans == 3:          ans = 'c'     elif ans == 4:          ans = 'd'    open(quiz_file) quiz_contents:      choices = [none] * num_questions      = 0      chunk in get_chunk(quiz_contents, 6):             if not chunk:             break              print()             line in chunk:             print(line)              choices[i] = int(input("1   3\n2    4?\n:"))             get_awns(choices[i]) 

i appreciate replies, whether it's criticism, tips or help, if it's relevant subject.

if read answer correctly, question how separate within each chunk question answers. @jonrsharpe commented, have access items of each chunk separately. either using subscripts jonrsharpe said, or can unpack them directly.

def readchunk(buf, n):     _ in range(n):         yield next(buf).strip() #get rid of newlines  open('questions.txt', 'r') lines:     while true:         try:             #unpacking separate values             correct, question, *answers = readchunk(lines, 6)         except valueerror:             #end of file             break         print('the question is', question)         print('and answers are', answers)         print('and correct answers is', answers[ord(correct) - ord('a')]) 

using sample file, snippet outputs:

the question sport uses term love ? , answers ['a)tennis', 'b)golf', 'c)football', 'd)swimming'] , correct answers a)tennis question german word water ? , answers ['a)wodar', 'b)wasser', 'c)werkip', 'd)waski'] , correct answers b)wasser 

Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -