【问题标题】:printing, splitting, and replacing in lists在列表中打印、拆分和替换
【发布时间】:2015-02-04 00:24:03
【问题描述】:

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

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”选项之前添加一个新行,有没有办法像前四个选项一样将它组合在一起?

【问题讨论】:

    标签: python list random replace split


    【解决方案1】:

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

     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 中的每一行。

    【讨论】:

    • 导入字符串和string.ascii_lowercase到底是做什么的?只是为了不盲目做。
    • string 是一个包含很多与字符串相关的方法的模块,但我主要只是使用它,因为它有一个小写字符列表。 string.ascii_lowercase 正是它等于:'abcdefghijklmnopqrstuvwxyz' 这让它适用于最多 25 个答案的问题(加上'以上都不是')
    • 除非我误解了你的问题,否则我相信这就是这样做的,声明:chars1[i]+'。 '+answers[i],在每个答案的开头添加一个字符。抱歉,这显然解释得很糟糕,请在上面添加此内容
    • 非常感谢,我会努力确保成功实施。我没有看到该评论的完整回复,这只是一个误解。
    • 嗯,它在每个“以上都不是”之前添加了一个新行
    猜你喜欢
    • 1970-01-01
    • 2013-01-05
    • 1970-01-01
    • 1970-01-01
    • 2021-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多