Trivia Game and Choice Function

Question:

We are required to create a trivia gave that gives the user the term dates and term number of a Canadian Prime Minister and the player is required to enter the correct prime minister. Here is my code:

from random import*

pm=["Macdonald", "Mackenzie", "Macdonald", "Abbott", "Thompson", "Bowell",
"Tupper", "Laurier", "Borden", "Borden", "Meighen", "King", "Meighen", "King",
"Bennett", "King", "St-Laurent", "Diefenbaker", "Pearson", "Clark", "Trudeau",
"Turner","Mulroney", "Campbell", "Chretien", "Martin", "Harper"]

terms=["July 1, 1867 to Nov. 5, 1873", "Nov. 7, 1873 to Oct. 8, 1878",
   "Oct. 17, 1878 to June 6, 1891", "June 16, 1891 to Nov. 24, 1892",
   "Dec. 5, 1892 to Dec. 12, 1894", "Dec. 21, 1894 to April 27, 1896",
   "May 1, 1896 to July 8, 1896", "July 11, 1896 to Oct. 6, 1911",
   "Oct. 10, 1911 to Oct. 12, 1917", "Oct. 12, 1917 to July 10, 1920",
   "July 10, 1920 to Dec. 29, 1921", "Dec. 29, 1921 to June 28, 1926",
   "June 29, 1926 to Sept. 25, 1926", "Sept. 25, 1926 to Aug. 7, 1930",
   "Aug. 7, 1930 to Oct. 23, 1935", "Oct. 23, 1935 to Nov. 15, 1948",
   "Nov. 15, 1948 to June 21, 1957", "June 21, 1957 to Apr. 22, 1963",
   "Apr. 22, 1963 to Apr. 20, 1968", "Apr. 20, 1968 to June 4, 1979",
   "June 4, 1979 to March 3, 1980", "March 3, 1980 to June 30, 1984",
   "June 30, 1984 to Sept. 17, 1984", "Sept. 17, 1984 to June 25, 1993",
   "June 25, 1993 to Nov. 4, 1993", "Nov. 4, 1993 to Dec. 11, 2003",
   "Dec. 12, 2003 to Feb. 5, 2006", "Feb. 6, 2006 -"]

termnum=["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14",
     "15", "16", "17", "18", "19", "20", "21", "22", "23","24", "25", "26", "27", 
     "28"]
score=0
choiceterm=choice(terms)
choicePM=choice(pm)
choicetermnum=choice(termnum)

    for i in range(10):
        print(choiceterm[i], choicetermnum[i])
        ans=input("Which prime minister reigned in this time?:n")
        if ans == choicePM[i]:
        print("Great job!")
        score+=1
    else:
        print("Incorrect!  The corret answer was", choicePM[i])

Please ignore the fact that there is no final output that gives the user feedback on how they did. Currently, I am trying to use the choice function so that it will output a term date and term number that corresponds with the proper prime minister yet it does not output in the order of the list I created (Ex. “Term 1, July 1, 1867 to Nov. 5, 1873” and so on).
As of right now, this program outputs a single letter from term dates and a number which do not correspond with each other. Also, when the user enters an incorrect answer, the string index is out of range. I have tried many different things so far that have not worked. So my question is how would I get all of the variables to correspond with each other, yet not being in the order that I listed them in the list? If possible, I would also like some help understanding indices and “for i in…” part as I have a general knowledge of when to use it, but do not know exactly what it does. Thank you.

Asked By: D Shinas

||

Answers:

What do these three lines do:

choiceterm=choice(terms)
choicePM=choice(pm)
choicetermnum=choice(termnum)

?

Assuming terms[i] goes with pm[i] (my answer does not work otherwise), Try:

#remove those lines
...
for i in range(10): #this will ask first 10, but change this to something else like a random in range (0, len(terms)) 
    term = terms[i]
    realanswer = pm[terms.index(term)] #terms.index(terms[i]) gets the index of the term, then you look up the answer for that index 
    print(term)
    ans=input("Which prime minister reigned in this time?:n")
if ans == realanswer
    print("Great job!")
    score+=1
else:
    print("Incorrect!  The corret answer was {0}".format(realanswer))
Answered By: Tommy

You may want something like:

score=0
index = randint(1, 29)
choiceterm=terms[index]
choicePM=pm[index]
##print index, choiceterm, choicePM

for i in range(10):
    print(choiceterm)
    ans=input("Which prime minister reigned in this time?:n")
    if ans == choicePM:
        print("Great job!")
        score+=1
    else:
        print("Incorrect!  The corret answer was", choicePM)
Answered By: Christian Tapia

choiceterm=choice(terms) already gives you a string from the list. When you print choiceterm[i], you are just getting the first character of the string. print choiceterm will give you the whole string.

Answered By: Ned Zepplin
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.