-1

Im trying to create code that will choose a random number and then use that number to put the content of specific lines in a text file into a list.

I don't want a question to be added to the list more than once, so I made another list that would contain all the numbers that have been chosen. All the questions are on odd lines, and the answers are on even lines, so the generated number must also be even.

The code below is what I've tried to do, which doesn't run.

import random

#the empty question list
qlist=[0,0,0,0,0]
#the list that is filled with question numbers that have already been chosen
noschosen=[]
file=open('questiontest.txt')
lines=file.readlines()
i=0
#random question chooser
while i<len(qlist):
    chosen=False
    n=random.randint(1,10)
    for index in range(0,len(noschosen)):
        if n==noschosen[index]:
            chosen=True
    #all questions are on odd lines, so the random number can't be even.
    while n%2==0 or chosen==True:
        n=random.randint(1,10)
    #the number chosen is added to the chosen list
    noschosen.append(n)
    #the program adds the question and its answer to qlist
    qlist[i]=(lines[n],lines[n+1])
    #increment
    i=i+1

print (qlist)

This is what's in the next file:

.
Question1
Answer1
Question2
Answer2
Question3
Answer3
Question4
Answer4
Question5
Answer5
Question6
Answer6
Question7
Answer7
Question8
Answer8
Question9
Answer9
Question10
Answer10

The dot on the first line is intentional.

I expect the program to fill the list randomly in this fashion:

[('QuestionA', 'AnswerA'), 
 ('QuestionB', 'AnswerB'),
 ('QuestionC', 'AnswerC'),
 ('QuestionD', 'AnswerD'),
 ('QuestionE', 'AnswerE')]

The letters A. B. C, D and E represent any of the numbers from 1 to 10. For example, if the first n turned out to be 5, "Question3" and "Answer3" would be put into the first place (since line 5 is where Question3 is).

The Question number and its corresponding Answer should be grouped together. I'm not sure why my current code doesn't work, can anyone see the problem or how I could improve this code in general?

Prune
  • 76,765
  • 14
  • 60
  • 81
Tolu
  • 3
  • 2
  • Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. [Minimal, complete, verifiable example](http://stackoverflow.com/help/mcve) applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described. "code doesn't work" is not a problem specification. – Prune Jan 30 '19 at 00:20

2 Answers2

0

You are only checking if the number is already chosen on the first random that you get, after that you generate random numbers on this while

while n%2==0 or chosen==True:
    n=random.randint(1,10)

but never check if the new number is already chosen.

Also you could check if the number is chosen using if n in noschosen or make chosen = n in noschosen and avoid the for loop

piedra
  • 1,343
  • 14
  • 13
0

Most of all, you have line numbers that go up to 20, but you're choosing only from the first 10. Better yet, don't even bother allowing an even number: take a random number 1-10, and derive the line number from that:

q_line = 2*n - 1

The question is at lines[q_line] with the answer at the following line.


Before you attack 20 lines of code at once, you should learn the smaller techniques to make your life easier. Most of your code can be replaced by random.sample, a method that can simply return 5 random items from the list. If you combine your questions and answers into a list of pairs (tuples), then you can simply tell sample that you want to grab five pairs from that list. See here.

Prune
  • 76,765
  • 14
  • 60
  • 81