This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """Write a regular expression that can detect dates in the DD/MM/YYYY for- | |
| mat. Assume that the days range from 01 to 31, the months range from 01 | |
| to 12, and the years range from 1000 to 2999. Note that if the day or month | |
| is a single digit, it’ll have a leading zero. | |
| The regular expression doesn’t have to detect correct days for each | |
| month or for leap years; it will accept nonexistent dates like 31/02/2020 or | |
| 31/04/2021. Then store these strings into variables named month, day, and | |
| year, and write additional code that can detect if it is a valid date. April, | |
| June, September, and November have 30 days, February has 28 days, and | |
| the rest of the months have 31 days. February has 29 days in leap years. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ''' | |
| Write a program that asks users for their sandwich preferences. The pro- | |
| gram should use PyInputPlus to ensure that they enter valid input, such as: | |
| • Using inputMenu() for a bread type: wheat, white, or sourdough. | |
| • Using inputMenu() for a protein type: chicken, turkey, ham, or tofu. | |
| • Using inputYesNo() to ask if they want cheese. | |
| • If so, using inputMenu() to ask for a cheese type: cheddar, Swiss, | |
| or mozzarella. | |
| • Using inputYesNo() to ask if they want mayo, mustard, lettuce, or tomato. | |
| • Using inputInt() to ask how many sandwiches they want. Make sure this |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ''' | |
| To see how much PyInputPlus is doing for you, try re-creating the multipli- | |
| cation quiz project on your own without importing it. This program will | |
| prompt the user with 10 multiplication questions, ranging from 0 × 0 to | |
| 9 × 9. You’ll need to implement the following features: | |
| • If the user enters the correct answer, the program displays “Correct!” | |
| for 1 second and moves on to the next question. | |
| • The user gets three tries to enter the correct answer before the | |
| program moves on to the next question. | |
| • Eight seconds after first displaying the question, the question is |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ''' | |
| Extending the Multi-Clipboard | |
| Extend the multi-clipboard program in this chapter so that it has a delete | |
| <keyword> command line argument that will delete a keyword from the shelf. | |
| Then add a delete command line argument that will delete all keywords. | |
| ''' | |
| #! python3 | |
| # mcb.pyw - Saves and loads pieces of text to the clipboard. | |
| # Usage: py.exe mcb.pyw save <keyword> - Saves clipboard to keyword. | |
| # py.exe mcb.pyw <keyword> - Loads keyword to clipboard. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ''' | |
| Create a Mad Libs program that 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ''' | |
| Write a program that opens all .txt files in a folder and searches for any | |
| line that matches a user-supplied regular expression. The results should | |
| be printed to the screen. | |
| ''' | |
| import re | |
| from pathlib import Path | |
| import pyinputplus as pyip | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ''' | |
| Selective Copy | |
| Write a program that walks through a folder tree and searches for files with | |
| a certain file extension (such as .pdf or .jpg). Copy these files from whatever | |
| location they are in to a new folder. | |
| ''' | |
| import shutil, os | |
| def pathInputAndCheck(): | |
| while True: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ''' | |
| Deleting Unneeded Files | |
| It’s not uncommon for a few unneeded but humongous files or folders to | |
| take up the bulk of the space on your hard drive. If you’re trying to free up | |
| room on your computer, you’ll get the most bang for your buck by deleting | |
| the most massive of the unwanted files. But first you have to find them. | |
| Write a program that walks through a folder tree and searches for excep- | |
| tionally large files or folders—say, ones that have a file size of more than | |
| 100MB. (Remember that to get a file’s size, you can use os.path.getsize() from | |
| the os module.) Print these files with their absolute path to the screen. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ''' | |
| Filling in the Gaps | |
| Write a program that finds all files with a given prefix, such as spam001.txt, | |
| spam002.txt, and so on, in a single folder and locates any gaps in the num- | |
| bering (such as if there is a spam001.txt and spam003.txt but no spam002.txt). | |
| Have the program rename all the later files to close this gap. | |
| As an added challenge, write another program that can insert gaps | |
| into numbered files so that a new file can be added. | |
| ''' | |
| import pyinputplus as pyip |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ''' | |
| 2048 | |
| 2048 is a simple game where you combine tiles by sliding them up, down, | |
| left, or right with the arrow keys. You can actually get a fairly high score | |
| by repeatedly sliding in an up, right, down, and left pattern over and over | |
| again. Write a program that will open the game at https://gabrielecirulli | |
| .github.io/2048/ and keep sending up, right, down, and left keystrokes to | |
| automatically play the game. | |
| ''' |
OlderNewer