Pergunta

import random

file1 = open ("Questions_tf.txt","r")

q = [] #True/false questions
a = [] #True/false answers
dict1 = {}


with open ("Questions_tf.txt", "r") as file1:
        q = [line.strip() for line in file1]

with open ("tf_answers.txt", "r") as file2:
        a = [line.strip() for line in file2]

random_number = 0

def answers(a, file2):
    global tf_randno
    print a [random_number]
    print random_number
    return a

def Questions(q, file1, random_number):
    random_number = random.randrange(0, 5)
    print q[random_number]
    print random_number
    return q
    return random_number

def intro():
    print "Welcome to the game"

def main():
    intro()
    Questions(q, file1, random_number)
    answers(a, file2)


    file1.close()
 main()

Here's my latest code.

def main(questions, answers):
while len(questions) != 0:
    shuffle(questions)
    print questions.pop(r)
    print answers.pop(r)
    ask_true_false(questions)
    if response == answers.pop(r):

First off, my code is far from done, and at least how it is now is barely how it will turn out. But here's the start to the code anyway. How I have it now, I'm trying to make it where it will pick a random question from the text file that has the questions, and then the same number from the text file with the answers, so that they are the same. But I can't figure out why that is happening. Also, I tried doing it with classes, but couldn't get it right that way either. Is there a better way to do this?

Foi útil?

Solução

First of all, you don't have to initialize variables in Python. It will usually create them as needed. Just this code should be sufficient:

import random

with open('questions.txt', 'r') as question_file:
    questions = [line.strip() for line in question_file]

with open('answers.txt', 'r') as answer_file:
    answers = [line.strip() for line in answer_file]

# Make a list of tuples: [(q0, a0), (q1, a1), ...]
trivia = zip(questions, answers)
# Shuffle it in place
random.shuffle(trivia)
# Then iterate over it
for question, answer in trivia:
    # play the game
    pass

Hopefully you can see how this could be easily incorporated into your main. By the way, you can use:

if __name__ == '__main__':
    # code here
    pass

to define code that should run if your program is invoked on the command line. You also don't need to explicitly close files, as the with statement does this automatically.

Outras dicas

As each answer belongs to one question, you can keep them paired. You could read in one list qas (questions-and-answers) containing pairs of questions and answers. Then you can use random.choice to randomly select one.

Something along these lines:

import random

with open ('Questions_tf.txt', 'r') as qs, open ('tf_answers.txt', 'r') as ass:
    qas = [map (str.strip, qa) for qa in zip (qs, ass) ]

def randomQA ():
    return random.choice (qas)

for _ in range (10):
    question, answer = randomQA ()
    #actual quiz goes here
    print 'The question is', question
    print 'The answer is', answer
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top