使用for循环

时间:2017-09-12 15:36:29

标签: python file csv

在SO上没有什么相同,所以问题是:

我有这个文件结构:(csv文件名为test.csv)

1.What line is this?    1
2.What line is this?    2
3.What line is this?    3
4.What line is this?    4
5.What line is this?    5

我希望程序能够打印第一行(1.这是什么行?)然后询问用户输入(answer =?),如果答案等于相应列中的内容,则输出"右"

到目前为止的代码:

它打印问题并要求答案,但没有 1.输出"正确"如果是的话 2.转到下一个问题,如果正确

import csv
def main():


    with open('test.csv','r') as f:
        reader=csv.reader(f)
        for row in reader:
                  questions=row
                  print(questions)

                  answer=input("answer:")
                  answer_correct=False

                  for row in reader:
                      for field in row:
                                for i in range(len(questions)):
                                          if field==questions[i]:
                                                        currentindex=questions.index(field)
                                                        if row[currentindex+1]==answer:
                                                                answer_correct==True

    if answer_correct == False :
        print("Wrong answer, sorry!")
    else:
      print("****You're right!*****")

main()

如果您有答案,可以请:

  1. 建议修复代码,指出错误并解释解决方案
  2. 建议使用csv阅读器而不使用其他导入工具来解决问题的更优雅的修补程序/代码段。
  3. 如前所述,代码需要: - 出现每个问题,询问用户输入(回答),如果对或错(打印对错)然后转到下一个问题,直到文件结束

    到目前为止输出:

    ['1.What line is this?', '1']
    answer:1
    Wrong answer, sorry!
    >>>
    

    我也试过这个:

    导入csv

    def main():
    
    
        with open('test.csv','r') as f:
            reader=csv.reader(f)
            for row in reader:
                      print(row[0])
    
                      answer=input("answer:")
                      answer_correct=False
    
                      for row in reader:
                          for field in row:
                                              if answer==row[1]:
                                                  answer_correct==True
    
        if answer_correct == False :
            print("Wrong answer, sorry!")
        else:
          print("****You're right!*****")
    
    main()
    

2 个答案:

答案 0 :(得分:1)

我写了一些小片段,它有用。

import csv
with open('questions.csv', 'r') as f:
    reader = csv.reader(f)
    for row in reader:
        question, correct_answer = row
        print(question)

        answer = input()
        if answer == correct_answer:
            print("Wow! Su much knowledge!")
        else:
            print("Still trying")

好像你的代码中有错误的缩进 检查你的答案的块将在整个周期后执行。

答案 1 :(得分:1)

假设您的CSV文件实际上有逗号,如下所示:

1.What line is this?,1
2.What line is this?,2
3.What line is this?,3
4.What line is this?,4
5.What line is this?,5

你可以按如下方式重做:

import csv

def main():
    with open('test.csv', 'r', newline='') as f_input:
        for question, correct_answer in csv.reader(f_input):
            answer = input(question)

            if answer == correct_answer:
                print("**** You're right! ****")
            else:
                print("Wrong answer, sorry!")

        print("All done")

main()

例如:

1.What line is this?1
**** You're right! ****
2.What line is this?5
Wrong answer, sorry!
3.What line is this?7
Wrong answer, sorry!
4.What line is this?4
**** You're right! ****
5.What line is this?5
**** You're right! ****
All done

这使用单个for循环来迭代您的问题。如您所见,使用csv.reader()时,您将逐个迭代这些条目。每次执行此操作时,都会提升文件中的位置。在您的代码中,您有两个这样的for循环。结果是第一个读取一个条目,下一个循环然后读取更多,它不再从顶部开始。那么问题是第一个现在也继续前进,即它们不是独立的。所以你的主要问题是只有两批for row in reader

注意:在Python 3中,使用csv.reader()时,您应该使用newline=''打开文件。