0

我们需要创建一个琐事,为用户提供加拿大总理的任期日期和任期号,并且玩家需要输入正确的总理。这是我的代码:

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])

请忽略这样一个事实,即没有最终输出可以为用户提供有关他们如何做的反馈。目前,我正在尝试使用选择功能,以便它输出与合适的总理相对应的任期日期和任期编号,但它不会按照我创建的列表的顺序输出(例如“第 1 学期,7 月 1 日, 1867 年至 1873 年 11 月 5 日”等)。截至目前,该程序从学期日期输出一个字母和一个彼此不对应的数字。此外,当用户输入错误答案时,字符串索引超出范围。到目前为止,我已经尝试了许多不同的方法,但都没有奏效。所以我的问题是如何让所有变量相互对应,而不是按照我在列表中列出的顺序?如果可能的话,我还需要一些帮助来理解索引和“for i in ...”部分,因为我对何时使用它有一般的了解,但不知道它到底做了什么。谢谢你。

4

3 回答 3

0

这三行是做什么的:

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

?

假设 terms[i] 与 pm[i] 一起使用(否则我的答案不起作用),请尝试:

#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))
于 2013-10-31T03:43:23.550 回答
0

你可能想要这样的东西:

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)
于 2013-10-31T03:57:22.190 回答
0

choiceterm=choice(terms)已经为您提供了列表中的字符串。当您打印choiceterm[i] 时,您只是得到字符串的第一个字符。 print choiceterm会给你整个字符串。

于 2013-10-31T04:21:47.503 回答