【发布时间】:2014-03-27 21:50:27
【问题描述】:
import random
players = 0
player1 = 0
player2 = 0
def open_file(file_name, mode):
try:
the_file = open(file_name, mode)
except(IOError), e:
print 'Cannot open file', file_name + '. Try moving its location.'
raw_input('\nPress enter to exit. ')
sys.exit()
else:
return the_file
def next_line(the_file):
line = the_file.readline()
line = line.replace('/', '\n')
return line
def main():
file = open_file('Trivia_Questions.txt', 'r')
questions = file.read().split('\n\n')
file.close()
random.shuffle(questions)
for question in questions.splitlines():
if next_line(question) == 'Multiple Choice':
subject, question, answer1, answer2, answer3, answer4, reason, empty = map(question)
print subject
print question
print '1 -', answer1
print '1 -', answer1
print '1 -', answer1
print '1 -', answer1
print reason
subject, question, answer1, answer2, answer3, answer4, reason, empty = next_block(file)
else:
subject, question, answer1, answer2, reason, empty = map(question)
print subject
print question
print '1 -', answer1
print '2 -', answer2
print reason
print empty
subject, question, answer1, answer2, reason, empty = next_block(file)
main()
我一直在寻找如何去做这件事,但不知道该怎么做。 当我尝试运行此代码时,我得到了
AttributeError: 'list' object has no attribute 'splitlines'
我的 txt 文件设置如下 多选 问题 答案1 答案2 答案3 答案4 正确答案# 原因
真/假 问题 吨 F 正确的 t 或 f 原因
重复 12 次p>
在我决定询问之前,我已经在网上搜索了 2 天,因此我们将不胜感激。我需要正确地让一个琐事游戏随机选择一个问题,无论是多选的真假,而不是再次使用该问题。
【问题讨论】:
-
你可能会发现我的回答here很有用
标签: python python-2.6