对于我的课程作业,我们必须创建一个Python测验,要求用户输入状态的大写字母。他们的视频给了我们答案,但即使输入它,因为他们显示它我不断收到错误,我的程序将无法运行。我的教授要到星期一才开始,我希望有人可以帮助我找到我的错误。我想我要么是拼写错误的单词还是IDK ......我一直盯着它看几个小时,一行一行看起来和给出的例子一样。
感谢您的帮助。
这是我的代码:
# Constant for the number of states to quiz the user on.
NUM_STATES = 5
def main ():
# Initialize the state_caps dictionary.
state_caps = state_cap_dictionary ()
# Initial variables to keep count of the number
# of correct and incorrect answers.
correct = 0
incorrect = 0
#Quiz the user.
for count in range (NUM_STATES) :
# Get a random entry from the dictionary.
state, capital = state_caps.popitem ()
#Quiz the user.
print ('What is the capital of ', state, '? ', end=' ')
response = input ()
# Is the user correct?
if response.lower () == capital.lower ():
correct += 1
print ('Correct!')
else:
incorrect += 1
print ('Incorrect.')
#Display the results
print ('Correct responses: ', correct)
print ('Incorrect answers: ', incorrect)
def state_cap_dictionary ():
sc = { 'Alabama' : 'Montgomery' ,
'Alaska' : 'Juneau' ,
'Arizona' : 'Phoenix' ,
'Arkansas' : 'Little Rock' ,
'California' : 'Sacremento' ,
'Colorado' : 'Denver' ,
'Conneticut' : 'Hartford' ,
'Delaware' : 'Dover' ,
'Florida' : 'Tallahassee' ,
'Georgia' : 'Atlanta' ,
'Hawaii' : 'Honolulu' ,
'Idaho' : 'Boise' ,
'Illinois' : 'Springfield' ,
'Indiana' : 'Indianapolis' ,
'Iowa' : 'Des Moines' ,
'Kansas' : 'Topeka' ,
'Kentucky' : 'Frankfurt' ,
'Lousianna' : 'Baton Rouge' ,
'Maine' : 'Augusta' ,
'Maryland' : 'Annapolis' ,
'Massachusetts' : 'Boston' ,
'Michigan' : 'Lansing' ,
'Minnesota' : 'Saint Paul' ,
'Mississippi' : 'Jackson' ,
'Missouri' : 'Jefferson City' ,
'Montana' : 'Helena' ,
'Nebraska' : 'Lincoln' ,
'Nevada' : 'Carson City' ,
'New Hampshire' : 'Concord' ,
'New Jersey' : 'Trenton' ,
'New Mexico' : 'Santa Fe' ,
'New York' : 'Albany' ,
'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' : 'Houston' ,
'Utah' : 'Salt Lake City' ,
'Vermont' : 'Montpelier' ,
'Virginia' : 'Richmond' ,
'Washington' : 'Olympia' ,
'West Virginia' : 'Charleston' ,
'Wisconsin' : 'Madison' ,
'Wyoming' : 'Cheyenne' }
return sc
main ()
答案 0 :(得分:1)
这个缩进对我有用:
# Constant for the number of states to quiz the user on.
NUM_STATES = 5
def main():
# Initialize the state_caps dictionary.
state_caps = state_cap_dictionary()
# Initial variables to keep count of the number
# of correct and incorrect answers.
correct = 0
incorrect = 0
#Quiz the user.
for count in range(NUM_STATES) :
# Get a random entry from the dictionary.
state, capital = state_caps.popitem()
#Quiz the user.
print('What is the capital of ', state, '? ', end=' ')
response = input()
# Is the user correct?
if response.lower() == capital.lower():
correct += 1
print('Correct!')
else:
incorrect += 1
print('Incorrect.')
# Display the results
print ('Correct responses: ', correct)
print ('Incorrect answers: ', incorrect)
def state_cap_dictionary():
sc = { 'Alabama' : 'Montgomery' ,
'Alaska' : 'Juneau' ,
'Arizona' : 'Phoenix' ,
'Arkansas' : 'Little Rock' ,
'California' : 'Sacremento' ,
'Colorado' : 'Denver' ,
'Conneticut' : 'Hartford' ,
'Delaware' : 'Dover' ,
'Florida' : 'Tallahassee' ,
'Georgia' : 'Atlanta' ,
'Hawaii' : 'Honolulu' ,
'Idaho' : 'Boise' ,
'Illinois' : 'Springfield' ,
'Indiana' : 'Indianapolis' ,
'Iowa' : 'Des Moines' ,
'Kansas' : 'Topeka' ,
'Kentucky' : 'Frankfurt' ,
'Lousianna' : 'Baton Rouge' ,
'Maine' : 'Augusta' ,
'Maryland' : 'Annapolis' ,
'Massachusetts' : 'Boston' ,
'Michigan' : 'Lansing' ,
'Minnesota' : 'Saint Paul' ,
'Mississippi' : 'Jackson' ,
'Missouri' : 'Jefferson City' ,
'Montana' : 'Helena' ,
'Nebraska' : 'Lincoln' ,
'Nevada' : 'Carson City' ,
'New Hampshire' : 'Concord' ,
'New Jersey' : 'Trenton' ,
'New Mexico' : 'Santa Fe' ,
'New York' : 'Albany' ,
'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' : 'Houston' ,
'Utah' : 'Salt Lake City' ,
'Vermont' : 'Montpelier' ,
'Virginia' : 'Richmond' ,
'Washington' : 'Olympia' ,
'West Virginia' : 'Charleston' ,
'Wisconsin' : 'Madison' ,
'Wyoming' : 'Cheyenne' }
return sc
main()
答案 1 :(得分:0)
你错过了一些缩进。 此版本适用于Python 3,链接here
你需要记住,函数A使用B,然后两者都需要有相同的缩进,在粘贴的代码中,state_cap_dictionary
是main的私有函数,并且你在声明之前使用它。
# Constant for the number of states to quiz the user on.
NUM_STATES = 5
def main ():
# Initialize the state_caps dictionary.
state_caps = state_cap_dictionary()
# Initial variables to keep count of the number
# of correct and incorrect answers.
correct = 0
incorrect = 0
#Quiz the user.
for count in range (NUM_STATES) :
# Get a random entry from the dictionary.
state, capital = state_caps.popitem()
#Quiz the user.
print ('What is the capital of ', state, '? ')
response = input()
# Is the user correct?
if response.lower() == capital.lower():
correct += 1
print ('Correct!')
else:
incorrect += 1
print ('Incorrect.')
#Display the results
print ('Correct responses: ', correct)
print ('Incorrect answers: ', incorrect)
def state_cap_dictionary ():
sc = { 'Alabama' : 'Montgomery' ,
'Alaska' : 'Juneau' ,
'Arizona' : 'Phoenix' ,
'Arkansas' : 'Little Rock' ,
'California' : 'Sacremento' ,
'Colorado' : 'Denver' ,
'Conneticut' : 'Hartford' ,
'Delaware' : 'Dover' ,
'Florida' : 'Tallahassee' ,
'Georgia' : 'Atlanta' ,
'Hawaii' : 'Honolulu' ,
'Idaho' : 'Boise' ,
'Illinois' : 'Springfield' ,
'Indiana' : 'Indianapolis' ,
'Iowa' : 'Des Moines' ,
'Kansas' : 'Topeka' ,
'Kentucky' : 'Frankfurt' ,
'Lousianna' : 'Baton Rouge' ,
'Maine' : 'Augusta' ,
'Maryland' : 'Annapolis' ,
'Massachusetts' : 'Boston' ,
'Michigan' : 'Lansing' ,
'Minnesota' : 'Saint Paul' ,
'Mississippi' : 'Jackson' ,
'Missouri' : 'Jefferson City' ,
'Montana' : 'Helena' ,
'Nebraska' : 'Lincoln' ,
'Nevada' : 'Carson City' ,
'New Hampshire' : 'Concord' ,
'New Jersey' : 'Trenton' ,
'New Mexico' : 'Santa Fe' ,
'New York' : 'Albany' ,
'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' : 'Houston' ,
'Utah' : 'Salt Lake City' ,
'Vermont' : 'Montpelier' ,
'Virginia' : 'Richmond' ,
'Washington' : 'Olympia' ,
'West Virginia' : 'Charleston' ,
'Wisconsin' : 'Madison' ,
'Wyoming' : 'Cheyenne' }
return sc
main ()