D
D
devolw2022-04-08 08:58:01
Python
devolw, 2022-04-08 08:58:01

Working with csv python, how to implement random string output?

There is a csv file containing - a list of questions, answer options and the correct answer for the game "Who wants to be a millionaire".

Myself
624fcd4befc2f399164260.png

You should implement a random output of a pair of rows from columns (allQuestion + allAnswer) and after the user's answer, compare the user's answer with the answer from correctAnswer.

So far, it has come down to how to display all rows from columns (allQuestion + allAnswer).
My piece of code:
# with open('millionerQuestions(1).csv', mode='r', encoding='utf-8-sig') as f:
# reader = csv.DictReader(f, delimiter=";" )
# for row in reader:
# print(row['allQuestion'], "\n", row['allAnswer']

)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Gornostaev, 2022-04-08
@devolw

Instead of outputting the data read from the file, they can be written to a list or dictionary, and then work with it, including getting a random element.

D
denislysenko, 2022-04-08
@denislysenko

import random # ипортируешь модуль рандом
# Вот пример как он работает со списком
city_list = ['New York', 'Los Angeles', 'Chicago', 'Houston', 'Philadelphia']
print("Выбор случайного города из списка - ", random.choice(city_list))



# пример как нужно сделать тебе
my_str = 'A:kart. B:Mobile. C:Shema. D:other'  # например: у тебя есть такая строка, но тебе нужно из нее получить список
my_list = (my_str.split('.'))  # делаем из строки список. То есть разбиваем строку по точке
print(my_list) # --> ['A:kart', ' B:Mobile', ' C:Shema', ' D:other'] 


#таким образом можно выбирать случайный ответ из вариантов ответа
result = random.choice(my_list)
print(result)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question