我的程序继续说列表索引超出范围

时间:2014-04-29 01:27:32

标签: python list python-multithreading

print "Ready For the Sports Trivia Game"
print 
print
import random
print "What sport would you like to be tested on"
print "Press 1 for Baseball"
print "Press 2 for Basketball" 
print "Press 3 for Hockey" 
choice = input()
answers = []


questions = open("questionsreadin.txt", "r")
lines = questions.readlines() 
print lines
print len(lines)
questions.close()
"string \n".strip();


ans = open("answersreadin.txt","r")
lines2 = ans.readlines()
print lines2
print len(lines2)
ans.close()
#"string \n".strip();
'''
questions1 = open("questionsreadin.txt", "r")
lines1 = questions1.readlines() 
print lines1
print len(lines1)
questions1.close()
"string \n".strip();

ans1 = open("answersreadin.txt","r")
lines3 = ans1.readlines()
print lines3
print len(lines3)
ans1.close()
#"string \n".strip();

questions4 = open("questionsreadin.txt", "r")
lines4 = questions4.readlines() 
print lines4
print len(lines4)
questions4.close()
"string \n".strip();

ans4 = open("answersreadin.txt","r")
lines5 = ans4.readlines()
print lines5
print len(lines5)
ans4.close()
#"string \n".strip();
'''

if choice == 1:
    for i in range(4): 
        questions = random.randint(0,3)
        if questions == 0:
            print "What is the name of the Toronto baseball team?"
            ans = raw_input()
            if ans == answers[0]:
                print True
                answers.pop(0)
                print answers
            else:
                print False 

        if questions == 1:
            print "Who won the last world series?"
            ans1 = raw_input()
            if ans1 == answers[1]:
                print True 
                answers.pop(1)
                print answers 
            else:
                print False  

        if questions == 2:
            print "How many baseball teams are in the MLB?" 
            ans2 = input() 
            if ans2 == answers[2]:
                print True 
                answers.pop(2)
            else:
                print False 

        if questions == 3:
            print "Who was the first African American MLB player?"
            ans3 = raw_input() 
            if ans3 == answers[3]:
                print True
                answers.pop(3)
            else:
                print False 

1 个答案:

答案 0 :(得分:0)

您的answers列表为空,只要您尝试从中引用元素(answers[0]answers[1]等),您就会收到此异常。

您需要先填写值,可能会从答案文件中填充它。你可能想要

answers = lines2