AlexZou_ 2022-02-11 14:33 采纳率: 50%
浏览 43
已结题

pythpn学习中关于自动生成美国各州小测试的程序(语言-python|开发工具-pycharm)

这个项目是《Python编程快速上手让繁琐工作自动化》第8章:读写文件 的第1个项目
主要实现自动生成35份试卷,每份试卷的内容是,美国各个州的首府的选择题,有4个选项,4个选项是乱排的,一共有50道题,对应美国50个州,但是每份试卷的题号也是乱排的
我是根据教辅一边思考一边写的,程序运行的时候只能创建第1个文件,而且第1个文件里面的内容是有问题的
我的代码如下:(编译器:pycharm)

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'}
    #Generate 35 quiz files
for quizNum in range(35):
    #TODO: Create the quiz and answer key files.
    quizfile=open("C:\\Users\\zouch\\PycharmProjects\\pythonProject1\\capitalquz%s.txt"%(quizNum+1), 'w')
    answerfile=open("C:\\Users\\zouch\\PycharmProjects\\pythonProject1\\capitalanswer%s.txt"%(quizNum+1), 'w')
    #TODO: Write out the header for the quiz.
    quizfile.write('Name:\nDate:\n\nPeriod:\n\n')
    quizfile.write((' '*20)+'States Captical Quiz(Form %s)'%(quizNum+1))
    quizfile.write('\n\n')
    #TODO: Shuffle the order of the states
    states=list(capitals.keys())
    random.shuffle(states)
    #TODO: Loop through all 50 states,making a question for each.
    for quesNum in range(50):
    #TODO: Get right and wrong answers
        correctAnswer=capitals[states[quesNum]]#这个还是词典
        wrongAnswer=list(capitals.values())
        del wrongAnswer[wrongAnswer.index(correctAnswer)]#删除value中correct answer的序号
        wrongAnswer=random.sample(wrongAnswer,3)
        answerOptions=wrongAnswer+list(correctAnswer)
        random.shuffle(answerOptions)
    #TODO:Write the question and answer options to the quiz file
    quizfile.write("%s.What's the capital of %s?\n"%(quizNum+1,states[quesNum]))
    for i in range(4):
        quizfile.write('%s. %s\n'%('ABCD'[i],answerOptions[i]))
    quizfile.write('\n')
    #TODO: Write the answer key to the file
    answerfile.write('%s. %s\n'%(quizNum+1,'ABCD'[answerOptions.index(list(correctAnswer))]))
    quizfile.close()
    answerfile.close()

我是根据教辅一边思考一边写的,程序运行的时候只能创建第1个文件,而且第1个文件里面的内容是有问题的,如下

img


错误如下:

img

简单的说,各州所对应的州府都没有办法在选项中体现,有时候选项会以完整的单词出现,有时候会被拆分成一个一个的字母,让我非常迷惑,找不到问题在哪里。而且当程序运行打印第1个question的时候就会出现问题,而答案文档却无法显示。
希望大家可以帮我看看问题出现在哪里
文件内容如下:

img

  • 写回答

4条回答 默认 最新

  • CSDN专家-HGJ 2022-02-11 15:20
    关注

    代码中的correctAnswer是一个字符串,用list转换就变成了单个字母,不要用list来转换,使用[]变为列表,在用index()时直接用整个字符串。
    代码第38行改成:
    answerOptions=wrongAnswer+[correctAnswer]
    第46行改成:
    answerfile.write('%s. %s\n'%(quizNum+1,'ABCD'[answerOptions.index(correctAnswer)]))
    改成如下即可:

    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'}
        #Generate 35 quiz files
    for quizNum in range(3):
        #TODO: Create the quiz and answer key files.
        quizfile=open("capitalquz%s.txt"%(quizNum+1), 'w')
        answerfile=open("capitalanswer%s.txt"%(quizNum+1), 'w')
        #TODO: Write out the header for the quiz.
        quizfile.write('Name:\nDate:\n\nPeriod:\n\n')
        quizfile.write((' '*20)+'States Captical Quiz(Form %s)'%(quizNum+1))
        quizfile.write('\n\n')
        #TODO: Shuffle the order of the states
        states=list(capitals.keys())
        random.shuffle(states)
        #TODO: Loop through all 50 states,making a question for each.
    
        for quesNum in range(50):
        #TODO: Get right and wrong answers
            correctAnswer=capitals[states[quesNum]]#这个还是词典
            wrongAnswer=list(capitals.values())
            del wrongAnswer[wrongAnswer.index(correctAnswer)]#删除value中correct answer的序号
            wrongAnswer=random.sample(wrongAnswer,3)
            answerOptions=wrongAnswer+[correctAnswer]
            print(answerOptions)
            random.shuffle(answerOptions)
        #TODO:Write the question and answer options to the quiz file
        quizfile.write("%s.What's the capital of %s?\n"%(quizNum+1,states[quesNum]))
        for i in range(4):
            quizfile.write('%s. %s\n'%('ABCD'[i],answerOptions[i]))
        quizfile.write('\n')
        #TODO: Write the answer key to the file
        answerfile.write('%s. %s\n'%(quizNum+1,'ABCD'[answerOptions.index(correctAnswer)]))
    quizfile.close()
    answerfile.close()
    

    如有帮助,请点采纳。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 2月19日
  • 已采纳回答 2月11日
  • 修改了问题 2月11日
  • 修改了问题 2月11日
  • 展开全部

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿