Python显示invalid syntax?
importrandomcapitals={'Shanxi':'TaiYuan','Shaanxi':'XiAn','HeBei':'ShiJiaZhuang','Jia...
import randomcapitals={'Shanxi':'TaiYuan','Shaanxi':'XiAn','HeBei':'ShiJiaZhuang','JiangXi':'NanChang','HuBei':'WuHan','HuNan':'ChangSha','JiangSu':'NanJing','ZheJiang':'HangZhou'}for cityNum in range(6): cityFile=open('capitalscity%s.txt'%(cityNum+1),'w') answerKeyFile=open('capitalscity_answer%s.txt'%(cityNum+1),'w') cityFile.write('Name:\n\nDate:\n\nPeriod:\n\n') cityFile.write((' '*20)+'Province Capitals City(Form %s)'%(cityNum+1)) cityFile.write('\n\n') province=list(capitals.keys()) random.shuffle(province) for questionNum in range(8): correctAnswer=capitals[province[questionNum]] wrongAnswers=list(capitals.values()) del wrongAnswers[wrongAnswers.index(correctAnswer)] wrongAnswers=random.sample(wrongAnswers,3) answerOptions=wrongAnswers+[correctAnswer] random.shuffle(answerOptions) #for questionNum in range(8): cityFile.write('%s.What is the capital of %s?\n'%(questionNum+1,province[questionNum])) for i in range(4): cityFile.write(' %s. %s\n'%('ABCD'[i],answerOptions[i])) cityFile.write('\n') answerKeyFile.write('%s. %s\n'%(questionNum+1,'ABCD'[answerOptions.index(correctAnswer)]) cityFile.close() answerKeyFile.close()
展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询