使用循环对 Python 测验进行评分

Scoring a Python quiz with a loop

这里我有一个简单测验的程序,我一直在尝试找到一种方法来给它打分,但无论你在测验中得到什么,它都说我的分数是 1。

我只能找到多项选择测验的评分方法,但这个测验使用字典和循环。

    Capitalquiz = {
'Alabama' : 'Montgomery',
'Alaska' : 'Juneau',
'Arizona' : 'Phoenix',
'Arkansas' : 'Little Rock',
'California' : 'Sacramento',
'Colorado' : 'Denver',
'Connecticut' : 'Hartford',
'Delaware' : 'Dover',
'Florida' : 'Tallahasse',
'Georgia' : 'Atlanta',
'Hawaii' : 'Honolulu',
'Idaho' : 'Boise',
'Illinois' : 'Springfield',
'Indiana' : 'Indianapolis',
'Iowa' : 'Des Moines',
'Kansas' : 'Topeka',
'Kentucky' : 'Frankfort',
'Louisiana' : 'Baton Rouge',
'Maine' : 'Augusta',
'Maryland' : 'Annapolis',
'Massachusettes' : 'Boston',
'Michigan' : 'Lansing',
'Minnesota' : 'St.Paul',
'Mississippi' : 'Jackson',
'Missouri' : 'Jefferson City',
'Montana' : 'Helena',
'Nebraska' : 'Lincoln',
'Nevada' : 'Carson City',
'New Hampshire' : 'Concord',
'New Jersey' : 'Trenton',
'New York' : 'Albany',
'New Mexico' : 'Santa Fe',
'North Carolina' : 'Raleigh',
'North Dakota' : 'Bismarck',
'Ohio' : 'Columbus',
'Oklahoma' : 'Oklahoma City',
'Oregon' : 'Salem',
'Pennsylvania' : 'Harrisburg',
'Rhode Island' : 'Providence',
'South Carolina' : 'Columbia',
'South Dakota' : 'Pierre',
'Tennessee' : 'Nashville',
'Texas' : 'Austin',
'Utah' : 'Salt Lake City' ,
'Vermont' : 'Montpelier',
'Virginia' : 'Richmond',
'Washington' : 'Olympia',
'West Virginia' : 'Charleston',
'Wisconsin' : 'Madison',
'Wyoming' : 'Cheyenne',
}

import sys
print ("Welcome to the State Capitals quiz to complete this quiz type the capitals of the states provided.")
begin = input("Would you like to begin?: ")
if begin == "yes":
    print(" ")



else:
    print ("Ok please try again later!")
    sys.exit()








import random

states = list(Capitalquiz.keys())
for i in [1, 2, 3, 4, 5]:
    state = random.choice(states)
    capital = Capitalquiz[state]
    capital_guess = input("What is the capital of " + state + "? ")



    if capital_guess == capital:
        print ("That is correct good job!")
        score = + 1


    else:
        print("That is not correct. The capital of " + state + " is " + capital + ".")






print("You got ", score, "out of 5 points")
print("Quiz Complete.")

我认为这是你在计分器上的语法。 尝试:

score = score + 1

而不是

score = +1

因为这会不断将分数重置为 1。

此外,您需要在 运行 测验之前将分数初始设置为零(例如,在导入随机语句之后:分数 = 0)