尝试运行我的代码时,我遇到了一个非常令人困惑的“扫描字符串文字时出现EOL”错误。它指向的位不在第15行上,删除第15行无济于事。字典中没有EOL或保留字符(在文本编辑器中选中)。
我到底做了什么?
File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\myScripts\Quiz\quiz.py", line 15
'Carson City', 'New Hampshire': 'Concord', 'New Jersey': 'Trenton', 'New
^
SyntaxError: EOL while scanning string literal
#! python3
# this code generates a random quiz for each member of a class
import random
capitals = {'Alabama': 'Montgomery', 'Alaska': 'Juneau', 'Arizona': 'Phoenix','Arkansas': 'Little Rock', 'California': 'Sacramento', 'Colorado': 'Denver','Connecticut': 'Hartford', 'Delaware': 'Dover', 'Florida': 'Tallahassee','Georgia': 'Atlanta', 'Hawaii': 'Honolulu', 'Idaho': 'Boise', 'Illinois':'Springfield', 'Indiana': 'Indianapolis', 'Iowa': 'Des Moines', 'Kansas':'Topeka', 'Kentucky': 'Frankfort', 'Louisiana': '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': 'Austin', 'Utah': 'Salt Lake City', 'Vermont':'Montpelier', 'Virginia': 'Richmond', 'Washington': 'Olympia', 'West Virginia': 'Charleston', 'Wisconsin': 'Madison', 'Wyoming': 'Cheyenne'}
# creates 35 quizzes
for quizNum in range(35):
# creates the files
quizfile = open('statesquiz%.txt' % (quizNum + 1),'w')
answerFile = open('statesquiz%_answers.txt' % (quizNum + 1),'w')
# writes some stuff in quiz files
quizFile.write('Name: \n\nDate: \n\n')
quizFile.write((' ' * 20) + 'State Capitals Quiz (Form %s)' % (quizNum + 1))
quizFile.write('\n\n')
# shuffles states
states = list(capitals.keys())
random.shuffle(states)
# loop through states and make a question for each
for questionNum in range(50):
# gets right and decoy answers
correctAnswer = capitals[states[questionNum]]
wrongAnswers = list(capitals.values())
del wrongAnswers[wrongAnswers.index(correctAnswer)]
wrongAnswers = random.samples(wrongAnswers, 3)
answers = wrongAnswers + [correctAnswer]
random.shuffle(answers)
# writes more stuff in quiz file
quizFile.write('%s. What is the capital of %s?\n' % (questionNum + 1, capitals[questionNum]))
for i in range(4):
quizFile.write(' %s. %s\n' % ('ABCD'[i], answers[i]))
quizFile.write('\n')
# writes stuff in answer file
answerFile.write('%s. %s\n' % (questionNum + 1, 'ABCD'[answers.index(correctAnswer)]))
quizFile.close()
answerFile.close()