我们需要创建一个琐事,给用户一个加拿大总理的日期和期限号码,玩家必须进入正确的总理。这是我的代码:
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 in in ...”部分,因为我对何时使用它有一般知识,但不知道它究竟是做什么的。谢谢。
答案 0 :(得分:0)
这三条线做了什么:
choiceterm=choice(terms)
choicePM=choice(pm)
choicetermnum=choice(termnum)
假设条款[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))
答案 1 :(得分: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)
答案 2 :(得分:0)
choiceterm=choice(terms)
已经从列表中提供了一个字符串。当你打印choiceterm [i]时,你只是得到字符串的第一个字符。 print choiceterm
将为您提供整个字符串。