added Chapter 8
This commit is contained in:
10
practice_projects/.vscode/settings.json
vendored
10
practice_projects/.vscode/settings.json
vendored
@@ -1,10 +0,0 @@
|
||||
{
|
||||
"python.pythonPath": "/usr/local/bin/python3",
|
||||
"python.linting.enabled": true,
|
||||
"python.linting.pylintEnabled": false,
|
||||
"python.linting.pep8Enabled": true,
|
||||
"python.linting.pep8Path": "/usr/local/bin/pep8",
|
||||
"python.linting.lintOnSave": true,
|
||||
"python.formatting.provider": "yapf",
|
||||
"python.formatting.yapfPath": "/usr/local/bin/yapf"
|
||||
}
|
||||
@@ -1,2 +0,0 @@
|
||||
@python.exe BulletPointAdder.py %*
|
||||
@pause
|
||||
@@ -1,2 +0,0 @@
|
||||
@python.exe CharacterPictureGrid.py %*
|
||||
@pause
|
||||
@@ -1,2 +0,0 @@
|
||||
@python.exe Collatz.py %*
|
||||
@pause
|
||||
@@ -1,2 +0,0 @@
|
||||
@python.exe CommaConnection.py %*
|
||||
@pause
|
||||
@@ -1,2 +0,0 @@
|
||||
@python.exe DisplayInventory.py %*
|
||||
@pause
|
||||
@@ -1,2 +0,0 @@
|
||||
@python.exe Password.py %*
|
||||
@pause
|
||||
@@ -1,2 +0,0 @@
|
||||
@python.exe PhoneAndEmail.py %*
|
||||
@pause
|
||||
@@ -1,2 +0,0 @@
|
||||
@python.exe TestPrintTable.py %*
|
||||
@pause
|
||||
39
practice_projects/mad_libs.py
Normal file
39
practice_projects/mad_libs.py
Normal file
@@ -0,0 +1,39 @@
|
||||
#! python3
|
||||
'''
|
||||
mad_libs.py - reads in text files and lets the user add their own text anywhere
|
||||
the word ADJECTIVE, NOUN, ADVERB, or VERB appears in the text
|
||||
file.
|
||||
|
||||
For example, a text file may look like this:
|
||||
```
|
||||
The ADJECTIVE panda walked to the NOUN and then VERB.
|
||||
A nearby NOUN was unaffected by these events.
|
||||
```
|
||||
|
||||
The program would find these occurrences and prompt the user to
|
||||
replace them.
|
||||
|
||||
Enter an adjective:
|
||||
silly
|
||||
Enter a noun:
|
||||
chandelier
|
||||
Enter a verb:
|
||||
screamed
|
||||
Enter a noun:
|
||||
pickup truck
|
||||
|
||||
The following text file would then be created:
|
||||
```
|
||||
The silly panda walked to the chandelier and then screamed.
|
||||
A nearby pickup truck was unaffected by these events.
|
||||
```
|
||||
'''
|
||||
|
||||
import re
|
||||
|
||||
file_name = input("Enter the filename:")
|
||||
file_read = open(file_name)
|
||||
content = file_read.read()
|
||||
|
||||
for to_replace in re.compile(r'ADJECTIVE|NOUN|ADVERB|VERB').findall(content):
|
||||
content.replace(to_replace)
|
||||
27
practice_projects/mcb.pyw
Normal file
27
practice_projects/mcb.pyw
Normal file
@@ -0,0 +1,27 @@
|
||||
#! python3
|
||||
'''
|
||||
mcb.pyw - Saves and loads pieces of text to the clipboard.
|
||||
Usage: py mcb.pyw save <keyword> - Saves clipboard to keyword.
|
||||
py mcb.pyw delete <keyword> - Delete the content of keyword.
|
||||
py mcb.pyw <keyword> - Loads keyword to clipboard.
|
||||
py mcb.pyw list - Loads all keywords to clipboard.
|
||||
'''
|
||||
|
||||
import shelve
|
||||
import pyperclip
|
||||
import sys
|
||||
|
||||
mcbShelf = shelve.open("mcb")
|
||||
# Save clipboard content
|
||||
if len(sys.argv) == 3:
|
||||
if sys.argv[1].lower() == 'save':
|
||||
mcbShelf[sys.argv[2]] = pyperclip.paste()
|
||||
elif sys.argv[1].lower() == 'delete':
|
||||
del mcbShelf[sys.argv[2]]
|
||||
elif len(sys.argv) == 2:
|
||||
# List keywords and load content
|
||||
if sys.argv[1].lower() == 'list':
|
||||
pyperclip.copy(str(list(mcbShelf.keys())))
|
||||
elif sys.argv[1] in mcbShelf:
|
||||
pyperclip.copy(mcbShelf[sys.argv[1]])
|
||||
mcbShelf.close()
|
||||
64
practice_projects/random_quiz_generator.py
Normal file
64
practice_projects/random_quiz_generator.py
Normal file
@@ -0,0 +1,64 @@
|
||||
#! python3
|
||||
'''
|
||||
random_quiz_generator.py - creates quizzes with questions and answers
|
||||
in random order, along with the answer key.
|
||||
'''
|
||||
|
||||
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):
|
||||
# Create the quiz and answer key files.
|
||||
quizFile = open('capitalsquiz%s.txt' % (quizNum + 1), 'w')
|
||||
answerKeyFile = open('capitalsquiz_answers%s.txt' % (quizNum + 1), 'w')
|
||||
|
||||
# Write out the header for the quiz.
|
||||
quizFile.write('Name:\n\nDate:\n\nPeriod:\n\n')
|
||||
quizFile.write((' '*20) + 'State Capitals Quiz (Form %s)' % (quizNum + 1))
|
||||
quizFile.write('\n\n')
|
||||
|
||||
# Shuffle the order of the states.
|
||||
states = list(capitals.keys())
|
||||
random.shuffle(states)
|
||||
|
||||
# Loop through all 50 states, making a question for each.
|
||||
for questionNum in range(50):
|
||||
# Get right and wrong answers.
|
||||
correctAnswer = capitals[states[questionNum]]
|
||||
wrongAnswers = list(capitals.values())
|
||||
del wrongAnswers[wrongAnswers.index(correctAnswer)]
|
||||
wrongAnswers = random.sample(wrongAnswers, 3)
|
||||
answerOptions = wrongAnswers + [correctAnswer]
|
||||
random.shuffle(answerOptions)
|
||||
# Write the question and the answer options to the quiz file.
|
||||
quizFile.write('%s. What is the capital of %s?\n' % (questionNum + 1,
|
||||
states[questionNum]))
|
||||
for i in range(4):
|
||||
quizFile.write(' %s. %s\n' % ('ABCD'[i], answerOptions[i]))
|
||||
quizFile.write('\n')
|
||||
# Write the answer key to a file.
|
||||
answerKeyFile.write('%s. %s\n' % (questionNum + 1, 'ABCD'[
|
||||
answerOptions.index(correctAnswer)]))
|
||||
quizFile.close()
|
||||
answerKeyFile.close()
|
||||
1
test_mad_libs.txt
Normal file
1
test_mad_libs.txt
Normal file
@@ -0,0 +1 @@
|
||||
The ADJECTIVE panda walked to the NOUN and then VERB. A nearby NOUN was unaffected by these events.
|
||||
Reference in New Issue
Block a user