如何在Python中跳过测验中的第一行问题?

时间:2017-11-03 09:43:00

标签: python-3.x

我想跳过第一行问题。这是我的代码的一部分:

 newques="n"
    while newques=="n":
          file=open("questions.txt","r")
          found=False
          for line in file:
              split=line.split(",")
              question=split[0]
              options=split[1]
              correct=split[2]
              found=True
          file.close()
          if found==True:
              print(question)
              print(options)
              answer=input("Enter correct letter")
              if answer==correct:
                  print("Correct!")
              else:
                  print("Incorrect!")
          newques=input("next question press n")

问题在txt文件中,格式如下:

1)Who is Beyonce married to?,A = Skengdo   B = Jay Z,B
2)Who performed Bodak Yellow?,A = Cardi B   B =Kodak Black,A

1 个答案:

答案 0 :(得分:1)

我想你的questions.txt就是每个问题都以新的一行开头。您将文件读入数组(问题)。然后从循环内部访问该数组并每次前进一个问题(q)。

newques="n"
file=open("questions.txt","r")
questions = [line for line in file]

q = 0 # current question
while newques=="n":
      split=questions[q].split(",")
      question=split[0]
      options=split[1]
      correct=split[2].strip() # delete \n newline
      print(question)
      print(options)
      answer=input("Enter correct letter")
      if answer==correct:
          print("Correct!")
      else:
          print("Incorrect!")
      q += 1
      newques=input("next question press n")