I'm stuck on my assignment and I'd like some help on how I can make it do what it's supposed to.
The task is the program is a multiple choice quiz that needs to randomly select a question, display the answers in random order, and use a list to get it working. My problems are I can't get the questions to print at all. I can get the first line printed from the list, but it is incorrectly formatted and I can't get the program to recognize the correct answer as the correct answer.
The code I currently have is not finished, I understand, but it's as far as I could make it before I got stuck. Here is what I have so far:
qList = [
("Who was the first president to hold a press conference on television?", "John F. Kennedy", "Dwight D. Eisenhower", "Richard M. Nixon", "Lyndon B. Johnson"),
("Which president served the shortest term (dying 32 days after taking the oath of office)?", "William Henry Harrison", "Chester A. Arthur", "William Taft", "Zachary Taylor"),
("Who was the first president to be born in a hospital?", "Jimmy Carter", "Gerald Ford", "George H. W. Bush", "Martin Van Buren"),
("Who was the first president to see the Pacific Ocean?", "Ullyses S. Grant", "Franklin D. Roosevelt", "Warren G. Harding", "Theodore Roosevelt"),
("Which president served two non-consecutive terms?", "Grover Cleveland", "Franklin Pierce", "Millard Fillmore", "Zachary Taylor"),
("Who was the first president to be impeached by the House of Representatives?", "Andrew Johnson", "Bill Clinton", "Donald Trump", "John Tyler"),
("Which president got stuck in the White House bathtub?", "William Howard Taft", "John Quincy Adams", "Teddy Roosevelt", "Millard Fillmore"),
("Which president never married? (His niece served as the White House hostess in lieu of a First Lady.)", "James Buchanan", "James Polk", "James Madison", "James Monroe"),
("Who was the first president to speak with astronauts on the moon?", "Richard Nixon", "John F. Kennedy", "Lyndon B. Johnson", "Jimmy Carter"),
("Who was the only person to serve as Vice-President and President, but elected to neither office?", "Gerald Ford", "Andrew Johnson", "Grover Cleveland", "Herbert Hoover"),
]
correct = 0
incorrect = 0
score = correct + incorrect
def readData():
try:
datafile = open("presidential_trivia.txt",'r')
line = datafile.readline().rstrip("\n")
line = datafile.readline().rstrip("\n")
datafile.close()
except ValueError:
print("Something went wrong. File not found.")
def AskQuestions():
global question, answer, wrong1, wrong2, wrong3
for question in qList:
question = []
idx = random.randint(0, len(qList)-1)
while used[idx]:
idx = random.randint(0, len(qList)-1)
print(idx)
## if len(qList) > 0:
## rq = random.randint(0, len(qList)-1)
## for question in random.randint():
## order = qList.split()
## questions.append(mcquestion.Question())
## print(question.question)
## print("These are the possible answers:")
## randomAnswers()
userans = input("Please answer A, B, C, or D: ")
if userans == correct:
correct += 1
print("Yes! That's correct!")
else:
incorrect += 1
print("Sorry, that's incorrect...")
def main():
readData()
begin = input("Are you ready to begin? Y to start, N to exit: ").upper()
if begin == "Y":
AskQuestions()
randomAnswers()
if begin == "N":
print("Run the program again when you're ready! We'll be right here!")
else:
return main()
main()
And this is the second file that the program calls to.
class Question:
def __init__(self, question, correct, wrong1, wrong2, wrong3):
self.question = question
self.correct = correct
self.wrong1 = wrong1
self.wrong2 = wrong2
self.wrong3 = wrong3
def __init__():
self.__order = [correct, wrong1, wrong2, wrong3]
self.__correct_answer = "A"
def randomAnswers(self):
#This is to randomize the order of the questions and the answers then stores them
used = [False,False,False,False]#This is needed to insure distribution
letters = ["A","B","C","D"]
c = random.randint(0,3)
used[c] = True
self.__order[c] = self.__correct
self.__correct_answer = letters[c]
w1 = w2 = w3 = c
while used[w1]:
w1 = random.randint(0,3)
used[w1] = True
self.__order[w1] = self.__wrong1
while used[w2]:
w2 = random.randint(0,3)
used[w2] = True
self.__order[w2] = self.__wrong2
while used[w3]:
w3 = random.randint(0,3)
used[w3] = True
self.__order[w3] = self.__wrong3
def __str__(self):
#According to teacher, this will print the question and randomly ordered answers
self.randomAnswers()
#This is how the answers will be ordered after the question is printed
return self.__question + \
"\n A - " + self.__order[0] + \
"\n B - " + self.__order[1] + \
"\n C - " + self.__order[2] + \
"\n D - " + self.__order[3]
I'd really appreciate feedback and an explanation of what's wrong and how I can fix it. If you need any more info please let me know!
globalvariable? Such asglobal question, answer, wrong1, wrong2, wrong3- burningalc