text - How do I make a random word generator in python programming? -
im doing flash card game , must pull data out of text doc , make random word generator each word showing right , 2 wrong answers
import random f=open('z:/desktop/keywords.txt','r') lines=f.readlines() words= [] words.append(lines[0]) words.append(lines[2]) words.append(lines[4]) words.append(lines[6]) words.append(lines[7]) words.append(lines[8]) words.append(lines[10]) words.append(lines[12]) words.append(lines[14]) words.append(lines[16]) words.append(lines[18]) words.append(lines[20]) words.append(lines[22]) words.append(lines[24]) words.append(lines[26])
you haven't stated how right answer determined, i'm going assume file 1 question, first line correct, while remaining lines incorrect. illustrate how use python random module.
import random f=open('z:/desktop/keywords.txt','r') lines = f.readlines() correct = lines[0] wrong = lines[1:] words = [correct] + random.sample(wrong, 2) print "\n".join(words)
edit: assuming file format interlaced question-answer-question-answer-question-answer, suggested alexis:
import random f = open('z:/desktop/keywords.txt','r') lines = f.readlines() questions = lines[::2] answers = lines[1::2] def printcard(index): show_incorrects = 2 print "question:", questions[index] correct = answers[index] incorrect = random.sample(answers[:index] + answers[index+1:], show_incorrects) answers = [correct] + incorrect random.shuffle(answers) in answers: print " *", printcard(3)
Comments
Post a Comment