I am trying to create a question/answer program where the program asks a question from a file and then compares the answer but when I write the right one it still tells that its wrong.
import random
def Questionnaire():
a=1
i=1
while i <= 10 :
with open("mcq.txt", "r") as r:
list = r.readlines()
questions = random.choice(list)
questans = questions.split(";;")
question, answer = questans[0], questans[1]
print(f"Q{a} - {question}")
a,i=a+1,i+1
ans(answer)
def ans(answer):
mychoice =str(input("What is your answer ?"))
if mychoice != answer :
print(f"Wrong answer, the right one is : {answer} \n")
else :
print("Bravo, it is the right one ! \n")
Questionnaire()
Here in my second function, the if else statement returns always False, even when I changed to make !=
BTW the text page is like Who was the first president of the USA? A-Trump B-Clinton C-Lincoln D-George Washington;;D and it continues like so…
The file is like so
Who is the head of the new government in France in 1936? A-Thorez B-Blum C-Daladier D-Pétain ;; B
What is the slogan of the Popular Front? A- “With France, for the French” B- “liberty, equality, fraternity” C- “work, family, homeland” D- “bread, peace, freedom” ;; D
Which party does not make up the Popular Front? A-The Radical Party B-The SFIO C-The National Bloc D-The Communist Party ;; C
What event of 1934 contributed to the creation of the Popular Front? A-The Congress of Tours B-The riots of the leagues C-The Crash of Wall-Street D-The resignation of Clémenceau ;; B
What event is at the origin of the crisis of the 1930s? A-The Russian Revolution B-The Rise of Fascism C-The First World War D-The Wall Street Crash ;; D
Which of these countries is an island? A-France B-Netherlands C-Australia D-Korea ;; C
What is the color of Henri's white horse 4 A-Green B-Red C-Gray D-White ;; D
One byte corresponds to A-1bit B-4 bits C-8 bits D-32 bits ;; C
With 1 byte you can encode A-255 values B-8 values C-16 values D-256 values ;; D
What is Microsoft Windows? A-A type of computer B-An operating system C-A database management system ;; B
The binary system uses the basis A-2 B-10 C-8 D-16 E-None of the above is true. ;; A
In Greco-Roman mythology, the gods gathered on A-Mount Sinai B-Vesuvius C-Mount Olympus ;; C
In Greek mythology, Pegasus is A-A winged horse B-A man with an eagle's head C-A giant ;; A
In what year was François Mitterrand elected President of the French Republic? A-1974 B-1976 C-1981 D-1986 ;; C
Thanks
>Solution :
Fix
The is because, when using readlines() it gives your a list of string ending by \n char, so the answer string becomes A\n that is different of A. Also sometimes there is a space before answer letter. Use str.strip() to remove both
question, answer = questans[0], questans[1].strip()
Improve
- you open and read the whole file at each iteration : read once at beginning
random.choicecould give the same row at 2 different iteration : userandom.shuffleto randomize once, then take in orderaandidoes same, keep one- use
enumerateand a slice of your question list to do only X question
def Questionnaire(nb_question=10):
with open("test.txt", "r", encoding="utf-8") as r:
values = r.read().splitlines() # don't keep ending \n
random.shuffle(values) # randomize once
for i, questions in enumerate(values[:nb_question], start=1):
question, answer = questions.split(";;")
print(f"Q{i} - {question.strip()}")
ans(answer.strip())