I want to include questions in an external file using python. Then be able to randomly select a question and let the user enter in the answer, then select another random question? Also with a point system when answer is answered correctly. Any help would be much appreciated.
Asked
Active
Viewed 761 times
-3
-
Some missing info here: - What's the size of the file? - What does the structure look like? – qasimzee Nov 14 '18 at 17:33
-
Welcome to Stack Overflow! If you could provide some details such as what you've tried and the file size and structure, it will be much easier to assist you – C.Nivs Nov 14 '18 at 17:34
-
You should probably look into JSON format and the `json` module for reading such files. Then get started. Then come back when you have an actual question. – tobias_k Nov 14 '18 at 17:37
-
You must parse your file depending on it's structure, and your file should be structured in some way. You can use existing parsers like `json` (if your questions stored as json) or write your own, if your file is `.txt` – Vova Nov 14 '18 at 17:40
-
1This question has no [tag:python] in it. See [ask] and how to create a [mcve]. – Peter Wood Nov 14 '18 at 17:41
-
@qasimzee just a small file with five questions, thanks – John Nov 14 '18 at 19:11
-
@qasimzee just a small file with 5 questions, thanks – John Nov 14 '18 at 19:11
1 Answers
0
Supposing your file (questions.txt
) has the format:
Question1
Question2
Question3
you could use:
import random
f = open("questions.txt", "r")
data = f.readlines()
f.close()
question = random.choice(data).rstrip()
print(question)

curlpipesudobash
- 691
- 1
- 10
- 21
-
This is great, thanks. How would you add a point system, e.g 1 point for every correct answer – John Nov 14 '18 at 19:13