Chata
Chata

Reputation: 85

How to display one element at a time from a list without repeating the same element in Python

I have some questions in a text file which I want to read one by one. The text file is read questions put in a list. After discussing the first question which is displayed, the program should allow me to trigger for the next. Questions should be coming in random manner.

Now, I have this script which does the job, but each time I have to run the program, as if I am running it for the first time.

Below is my code:

import random

with open('questions.txt', 'r') as f:
text = f.read()

list_of_qns = text.splitlines()

if len(list_of_qns) != 0:
    question = random.choice(list_of_qns)
    print(question)
    list_of_qns.remove(question)

Please help me to make it run one question at a time without terminating it if the list still have some questions. Please also note that I remove the question which we are done with to prevent it from being displayed more than one in the same run.

Upvotes: 0

Views: 302

Answers (4)

Piyush Nalawade
Piyush Nalawade

Reputation: 1

import random

# reads questions text 
with open('questions.txt', 'r') as f:
text = f.read()

# converts into text format
list_of_qns = text.splitlines()

# length of list_of_questions
number_of_questions = len(list_of_questions)

# create dcitionary to check question printed or not
dict_of_questions_printed = dict()

# for not printed question mark intially as 1 
for question in list_of_qns:
    dict_of_questions_printed[question] = 1

# loop until number_of_questions is not equal to 0 
while number_of_questions != 0:

    # select random question
    question = random.choice(list_of_qns)

    # checks if question is printed or value in dictionary is 0 continues
    if not dict_of_questions_printed[question]: 
        continue

    # else prints question, marks dictionary value of question to 0 and  
    # decrements number of question 
    print(question)
    dict_of_questions_printed[question] = 0
    number_of_question -= 1
    continue

Upvotes: 0

Thierry Lathuille
Thierry Lathuille

Reputation: 24232

You can use random.shuffle that will shuffle your list of questions in place.

Also, you can use readlines to read your file directly as a list of lines, rather than reading it and splitting it later.

import random

with open('questions.txt', 'r') as f:
    questions = f.readlines()

random.shuffle(questions)

for question in questions:
    print(question)

If you want to have several sessions of questions, you could define a function:

def ask_all_questions:
    random.shuffle(questions)

    for question in questions:
        print(question)

Each time you call it, all questions will be asked again, each time in a random order.

Upvotes: 3

David
David

Reputation: 3036

Use while instead of if:

import random

with open('questions.txt', 'r') as f:
text = f.read()

list_of_qns = text.splitlines()

while len(list_of_qns) != 0:
    question = random.choice(list_of_qns)
    print(question)
    list_of_qns.remove(question)

While lets you loop until the condition is not met anymore.

For user input, you could use something like:

inp = input()

Upvotes: 0

DDGG
DDGG

Reputation: 1241

You can try this:

import random

with open('questions.txt', 'r') as f:
    text = f.read()

    list_of_qns = list(filter(None, text.splitlines()))

    while list_of_qns:
        question = random.choice(list_of_qns)
        print(question)
        input()
        list_of_qns.remove(question)

Press the Enter key to go to the next question.

Upvotes: 1

Related Questions