码迷,mamicode.com
首页 > 编程语言 > 详细

每日练习五:《Python编程快速上手+让繁琐工作自动化》第八章项目

时间:2019-01-14 20:10:47      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:val   使用   sipp   mon   color   art   random   col   connect   

假如你是一位地理老师,班上有 35 名学生,你希望进行美国各州首府的一个小测验。不妙的是,班里有几个坏蛋,你无法确信学生不会作弊。你希望随机调整问题的次序,这样每份试卷都是独一无二的,这让任何人都不能从其他人那里抄袭答案。

思路:

1.新建35张试卷,使用循环

2.每份试卷随机生成50道题:

  a.使用random中sample方法,随机获得一个州

  b.判断获得的州是否已被使用过,若使用过则重新获取

  c.通过州获得首府信息

  b.将正确信息与三个错误首府信息组合在一个list中,使用random中的shuffle方法打乱顺序

import random
"""
1.新建35张试卷
2.每份试卷中随机生成50道题目
3.没份题目生成ABCD四个选项,一个正确答案,3个错误答案
"""
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}
for quizNum in range(35):
    quizFile = open(capitalsquiz%s.txt % (quizNum + 1), w)
    quizFile.write(Name:\n\nDate:\n\nPeriod:\n\n)  # 试卷标题
    used = []  # 记录已使用过得州
    for questionNum in range(50):
        # 判断州是否已被出过题,出过去除此项
        while True:
            state = random.sample(list(capitals.keys()), 1)  # 随机获取州名称
            state = ‘‘.join(state)
            if state in used:
                continue
            else:
                used.append(state)
                break
        correctAnswer = capitals[state]  # 得到州的首府

        # 获取4个选项,一个正确,三个错误
        answer = list(capitals.values())
        # print(wrongAnswer)
        answer.remove(correctAnswer)  # 去除正确答案
        answer = random.sample(answer, 3)  # 错误答案中随机取三个
        # print(wrongAnswer)
        answer.append(correctAnswer)
        # print(answer)
        random.shuffle(answer)
        # print(answer)
        quizFile.write(%s. What is the capital of %s?\n % (questionNum + 1, correctAnswer))
        for i in range(4):
            quizFile.write(    %s.%s % (ABCD[i], answer[i]))
        quizFile.write(\n)
    quizFile.close()

 

每日练习五:《Python编程快速上手+让繁琐工作自动化》第八章项目

标签:val   使用   sipp   mon   color   art   random   col   connect   

原文地址:https://www.cnblogs.com/huwang-sun/p/10268464.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!