Python practice exercise: Generate random quiz paper files

Source: Internet
Author: User
Tags shuffle

Topic

If you are a geography teacher with 35 students in your class, you want to take a quiz in the capitals of the U.S. state. Unfortunately, there are a few bad guys in the class, and you can't be sure that students won't cheat. You want to randomly adjust the order of the questions so that each paper is unique, which makes it impossible for anyone to copy the answer from someone else. Of course, it's time-consuming and boring to do it by hand. Fortunately, you know some Python.
Here's what the program does:
? Create 35 different quiz papers.
? Create 50 multiple-choice questions for each paper, in a random order.
? Provide a correct answer to each question and 3 random error answers, in order of random.
? Write the quiz paper in 35 text files.
? Write the answer to a 35 text file.
This means that the code needs to do the following:
? Keep the states and their capitals in a dictionary.
? For quiz text files and answer text files, call open (), write (), and Close ().
? Use Random.shuffle () to randomly adjust the order of problems and multiple options.

U.S. state capitals data in a dictionary

# the quiz data. Keys is states and values are their capitals.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 ', ' Ore Gon ': ' 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 ', ' WestVirginia ': ' Charleston ', ' Wisconsin ': ' Madison ', ' Wyoming ': ' Cheyenne '}

Quiz paper file, it needs to have a unique file name, and there is a certain standard title section, set aside location, let students fill in name, Date and class.
The file name of the quiz paper is Capitalsquiz. txt
Capitalsquiz . txt answers will be saved in a text file named Capitalsquiz_answers. txt .

Code
#!  python3# Randomquizgenerator.py-creates Quizzes with questions and answers in# random order, along with the answer key.# The quiz data. Keys is states and values are their capitals.import randomcapitals = {' Alabama ': ' Montgomery ', ' Alaska ': ' Juneau ', ' Arizo ' Na ': ' Phoenix ', ' Arkansas ': ' Little Rock ', ' California ': ' Sacramento ', ' Colorado ': ' Denver ', ' Connect Icut ': ' Hartford ', ' Delaware ': ' Dover ', ' Florida ': ' Tallahassee ', ' Georgia ': ' Atlanta ', ' Hawaii ': ' Honolulu ', ' I Daho ': ' 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 hamps ' Hire: ' Concord ', ' New Jersey ': ' Trenton ', ' New Mexico ': ' Santa Fe ', ' New York ': ' Albany ', ' North Carolina ': ' Raleigh ' Dakota ': ' Bismarck ', ' Ohio ': ' Columbus ', ' Oklahoma ': ' Oklahoma City ', ' Oregon ': ' Salem ', ' Pennsylvania ': ' Harrisburg ', ' Rhode ': ' Providence ', ' south of Carolina ': ' Columbia ', ' South Dakota ': ' Pierr E ', ' Tennessee ': ' Nashville ', ' Texas ': ' Austin ', ' Utah ': ' Salt Lake city ', ' Vermont ': ' Montpelier ', ' Virginia ': ' Richmond ', ' Washington ': ' Olympia ', ' West Virginia ': ' Charleston ', ' Wisconsin ': ' Madison ', ' wyomin '    G ': ' Cheyenne '}for quiznum in range (35): # Create quiz paper and answer file Quizfile = open (' capticalsquiz%s.txt '% (Quiznum + 1), ' W ') Answerkeyfile = open (' capticalsquiz_answers%s.txt '% (Quiznum + 1), ' W ') # Write title and start Quizfile.write (' name:\n\ndate:\n\n  Class:\n\n ') quizfile.write (' * + ' state capticals Quiz (from%s) '% (Quiznum + 1)) Quizfile.write (' \ n ') # Upset Capital Dictionary states = List (Capitals.keys ()) Random.shuffle (states) # Shuffle states Order # Loop states, manufacturing 50 questions for Questionnum in range (50): Correctanswer = Capitals[states[questionnum]] wronganswers = List (Capitals.values ()) del Wronganswers[wr        Onganswers.index (Correctanswer)] # Delete the correct answer in the list # Random.sample () function makes this choice easy, its first parameter is the list you want to select, and the second parameter is the number of values you want to select. Wronganswers = Random.sample (wronganswers, 3) answeroption = Wronganswers + [Correctanswer] Random.shuff Le (answeroption) # writes the problem Quizfile.write ('%s ') in the file.            What's the capital of%s?\n '% (questionnum + 1, States[questionnum])) for I in range (4):        Quizfile.write ('%s.%s\n '% (' ABCD ' [i], answeroption[i])) quizfile.write (' \ n ') # write an answer in the answer volume Answerkeyfile.write ('%s.%s\n '% (questionnum + 1, ' ABCD ' [Answeroption.index (Correctanswer)])) quizfile.cl OSE () Answerkeyfile.close ()

Python practice exercise: Generate a random quiz paper file

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.