python - printing, splitting, and replacing in lists -


i'm trying make quiz using questions file, , choice between 2 answer files. going have user choose between printing questions , answers, or few randomly generated ones. problem having trouble getting display intended. need display like:

1. how many... etc. a.answer b.answer  c.answer d.answer e.none of above 

but can't output right. text files consists of answers placed this:

c,3,4,5,6 a,4o,30,20,10 e,65,245,456,756 

so have replaced commas spaces, , have been succesful displaying them row rather 1 line using \n in place of spaces.. won't work answers more 1 word. need remove letter before answers (it's correct answer) , place list, , i'm unsure of how this.

import random   def main():     print("welcome garbology quiz \n")      quizfilecheck = input("first off, quiz file name? ")     while quizfilecheck != "questions.txt":           quizfilecheck = input("file not found.. correct quiz file name? ")      answerfilecheck = input("and answer file using? ")     while answerfilecheck != "american-answers.txt" , answerfilecheck != "metric-answers.txt":       answerfilecheck = input("file not found.. please enter correct answer file name. ")       questionlist = getdata()     answerlist = getformat()     inputanswers = printinputanswers(questionlist,answerlist)      def getdata():    open("questions.txt") questionfile:         questionlist = questionfile.readlines()     return questionlist  def getformat():     formatchoice = input("\n answers printed in metric or american format? (m or a): ")    formatchoice = formatchoice.lower()     while formatchoice != "a" , formatchoice != "m":       formatchoice = input("invalid input, please enter correct value (m or a): ")       formatchoice = formatchoice.lower()     if formatchoice == "a":       answerpath = "american-answers.txt"    else:       answerpath = "metric-answers.txt"     open(answerpath) answerfile:       answerlist = answerfile.readlines()          return answerlist  def printallanswers(questionlist,answerlist):    in range(0,len(questionlist)):       print(questionlist[i])       print(''.join(map(str,answerlist[i].replace(',',' ').replace(' ','\n'))))       allanswers = printallanswers(questionlist,answerlist)  def printinputanswers(questionlist,answerlist):     numofquestions = input(" how many questions want print? ")    if numofquestions.isdigit():       numofquestions = int(numofquestions)       in range(0,numofquestions):          randomnum = random.randint(0,len(questionlist))          print (questionlist[randomnum])          print(''.join(map(str,answerlist[randomnum].replace(',',' ').replace(' ',' '))))     main() 

***********output , know haven't called printallanswers() yet, i'm going after have correct output working

first off, quiz file name? questions.txt , answer file using? metric-answers.txt   answers printed in metric or american format? (m or a): m  how many questions want print? 4 if every country consumed , threw away @ rate americans do, how many planets' worth of resources required meet demand?  c 3 4 5 6  if every country consumed , threw away @ rate americans do, how many planets' worth of resources required meet demand?  c 3 4 5 6  america home 4 percent of world's children. percentage of world's toys americans buy , throw away?  4o 30 20 10  how many nonrecyclable styrofoam cups americans throw away in year?  d 5 billion 10 billion 15 billion 25 billion 

code

def printsingleanswer(questionlist,answerlist):    randomnum = random.randint(0,len(questionlist))    chars1= string.ascii_lowercase    answers=answerlist[randomnum].split(',')[1:] #removes answer letter    answers.append('none of above')    print ('\n'.join(chars1[i]+'. '+answers[i] in range(len(answers)))) 

output

 answers printed in metric or american format? (m or a): m a. 5 billion b. 10 billion c. 15 billion d. 25 billion  e. none of above 

it's adding new line before every "e" choice, there way keep grouped first 4 choices?

for answer sets not have commas, how about:

 import string  chars1= string.ascii_lowercase  answers=answerlist[randomnum].strip().split(',')[1:] #removes answer letter  answers.append('none of above')  print '\n'.join(chars1[i]+'. '+answers[i] in range(len(answers))) 

the statement:

 chars1[i]+'. '+answers[i],  

adds character beginning of each answer, , string.ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz' give alphabetic character every answer starting 'a'.

if there commas have store file full csv , use csv module load file , rather using split in code above, use each row extracted csv.


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -