在列表中打印,拆分和替换

时间:2014-12-05 18:38:51

标签: python list random replace split

我尝试使用问题文件进行测验,并在两个答案文件之间进行选择。我将让用户在打印所有问题和答案之间做出选择,或者只是随机生成一些问题和答案。我的问题是我无法按预期显示它。我需要它显示如下:

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

但我无法获得正确的输出。文本文件包含如下答案:

C,3,4,5,6
A,4O,30,20,10
E,65,245,456,756

所以我用空格替换了逗号,并且通过使用\ n代替空格成功地将它们显示为一行而不是一行...但它不会起作用,因为一些答案不止一个字。我还需要在答案之前删除这封信(这是正确的答案)并将其放入一个列表中,我真的不确定如何做到这一点。

import random


def main():
    print("Welcome to the Garbology Quiz \n")

    quizfilecheck = input("First off, what is the quiz file name? ")
    while quizfilecheck != "questions.txt":
          quizfilecheck = input("File not found.. what is the correct quiz file name? ")

    answerfilecheck = input("And now what answer file are you using? ")
    while answerfilecheck != "american-answers.txt" and answerfilecheck != "metric-answers.txt":
      answerfilecheck = input("File not found.. please enter the correct answer file name. ")


    questionList = getData()
    answerList = getFormat()
    inputanswers = printinputanswers(questionList,answerList)





def getData():
   with open("questions.txt") as questionFile:
        questionList = questionFile.readlines()

   return questionList

def getFormat():

   formatchoice = input("\n Would you like the answers printed in metric or american format? (m or a): ")
   formatchoice = formatchoice.lower()

   while formatchoice != "a" and formatchoice != "m":
      formatchoice = input("Invalid input, please enter a correct value (m or a): ")
      formatchoice = formatchoice.lower()

   if formatchoice == "a":
      answerPath = "american-answers.txt"
   else:
      answerPath = "metric-answers.txt"

   with open(answerPath) as answerFile:
      answerList = answerFile.readlines()



      return answerList

def printAllanswers(questionList,answerList):
   for i 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 do you want to print? ")
   if numofquestions.isdigit():
      numofquestions = int(numofquestions)
      for i in range(0,numofquestions):
         randomnum = random.randint(0,len(questionList))
         print (questionList[randomnum])
         print(''.join(map(str,answerList[randomnum].replace(',',' ').replace(' ',' '))))




main()

***********输出,我知道我还没有打电话给printallanswers(),在我输出正确的输出之后我会去

First off, what is the quiz file name? questions.txt
And now what answer file are you using? metric-answers.txt

 Would you like the answers printed in metric or american format? (m or a): m
 How many questions do you want to print? 4
If every country consumed and threw away at the rate Americans do, how many planets' worth of resources would be required to meet the demand?

C 3 4 5 6

If every country consumed and threw away at the rate Americans do, how many planets' worth of resources would be required to meet the demand?

C 3 4 5 6

America is home to 4 percent of the world's children. What percentage of the world's toys do Americans buy and throw away?

A 4O 30 20 10

How many nonrecyclable Styrofoam cups do Americans throw away in a year?

D 5 billion 10 billion 15 billion 25 billion

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

输出

 Would you like the 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 the above

在每个" e"之前添加一个新行。选择,有没有办法像前四个选项一样将它组合在一起?

1 个答案:

答案 0 :(得分:2)

对于没有逗号的答案集,如何:

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

声明:

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

在每个答案的开头添加一个字符,并且作为string.ascii_lowercase ='abcdefghijklmnopqrstuvwxyz',这将为每个以'a'开头的答案提供字母字符。

如果有逗号,那么你必须将文件存储为完整的csv并使用csv模块加载文件,然后不使用上面代码中的拆分,只需使用提取的csv中的每一行。