In this short and simple program, you can learn the secret and subtle art of keeping a gullible person busy for hours. I won’t spoil the punch line here. Copy the code and run it for yourself. This project is great for beginners, whether you’re smart or . . . not so smart.
When you run gullible.py, the output will look like this:
Gullible, by Al Sweigart [email protected]
Do you want to know how to keep a gullible person busy for hours? Y/N
> y
Do you want to know how to keep a gullible person busy for hours? Y/N
> y
Do you want to know how to keep a gullible person busy for hours? Y/N
> yes
Do you want to know how to keep a gullible person busy for hours? Y/N
> YES
Do you want to know how to keep a gullible person busy for hours? Y/N
> TELL ME HOW TO KEEP A GULLIBLE PERSON BUSY FOR HOURS
"TELL ME HOW TO KEEP A GULLIBLE PERSON BUSY FOR HOURS" is not a valid yes/no response.
Do you want to know how to keep a gullible person busy for hours? Y/N
> y
Do you want to know how to keep a gullible person busy for hours? Y/N
> y
Do you want to know how to keep a gullible person busy for hours? Y/N
> n
Thank you. Have a nice day!
To be more user friendly, your programs should attempt to interpret a range of possible inputs from the user. For example, this program asks the user a yes/no question, but it would be simpler for the player to simply enter “y” or “n” instead of enter the full word. The program can also understand the player’s intent if their caps lock key is activated, because it calls the lower()
string method on the string the player entered. This way, 'y'
, 'yes'
, 'Y'
, 'Yes'
, and 'YES'
, are all interpreted the same by the program. The same goes for a negative response from the player.
1. """Gullible, by Al Sweigart [email protected]
2. How to keep a gullible person busy for hours. (This is a joke program.)
3. This code is available at https://nostarch.com/big-book-small-python-programming
4. Tags: tiny, beginner, humor"""
5.
6. print('Gullible, by Al Sweigart [email protected]')
7.
8. while True: # Main program loop.
9. print('Do you want to know how to keep a gullible person busy for hours? Y/N')
10. response = input('> ') # Get the user's response.
11. if response.lower() == 'no' or response.lower() == 'n':
12. break # If "no", break out of this loop.
13. if response.lower() == 'yes' or response.lower() == 'y':
14. continue # If "yes", continue to the start of this loop.
15. print('"{}" is not a valid yes/no response.'.format(response))
16.
17. print('Thank you. Have a nice day!')
Try to find the answers to the following questions. Experiment with some modifications to the code and rerun the program to see what effect the changes have.
response.lower() == 'no'
on line 11 to response.lower() != 'no'
?while True:
on line 8 to while False:
?