Clickbait Headline Generator
Our website needs to trick people into looking at advertisements! But coming up with creative, original content is too hard. Luckily, with the clickbait headline generator, we can make a computer come up with millions of outrageous fake headlines. They’re all low quality, but readers don’t seem to mind. This program generates as many headlines as you need from a Mad Libs–style template.
clickbait_headline_generator.py
1"""Clickbait Headline Generator, by Al Sweigart al@inventwithpython.com
2A clickbait headline generator for your soulless content farm website.
3This code is available at https://nostarch.com/big-book-small-python-programming
4Tags: large, beginner, humor, word"""
5
6import random
7
8# Set up the constants:
9OBJECT_PRONOUNS = ['Her', 'Him', 'Them']
10POSSESIVE_PRONOUNS = ['Her', 'His', 'Their']
11PERSONAL_PRONOUNS = ['She', 'He', 'They']
12STATES = ['California', 'Texas', 'Florida', 'New York', 'Pennsylvania',
13 'Illinois', 'Ohio', 'Georgia', 'North Carolina', 'Michigan']
14NOUNS = ['Athlete', 'Clown', 'Shovel', 'Paleo Diet', 'Doctor', 'Parent',
15 'Cat', 'Dog', 'Chicken', 'Robot', 'Video Game', 'Avocado',
16 'Plastic Straw','Serial Killer', 'Telephone Psychic']
17PLACES = ['House', 'Attic', 'Bank Deposit Box', 'School', 'Basement',
18 'Workplace', 'Donut Shop', 'Apocalypse Bunker']
19WHEN = ['Soon', 'This Year', 'Later Today', 'RIGHT NOW', 'Next Week']
20
21
22def main():
23 print('Clickbait Headline Generator')
24 print('By Al Sweigart al@inventwithpython.com')
25 print()
26
27 print('Our website needs to trick people into looking at ads!')
28 while True:
29 print('Enter the number of clickbait headlines to generate:')
30 response = input('> ')
31 if not response.isdecimal():
32 print('Please enter a number.')
33 else:
34 numberOfHeadlines = int(response)
35 break # Exit the loop once a valid number is entered.
36
37 for i in range(numberOfHeadlines):
38 clickbaitType = random.randint(1, 8)
39
40 if clickbaitType == 1:
41 headline = generateAreMillenialsKillingHeadline()
42 elif clickbaitType == 2:
43 headline = generateWhatYouDontKnowHeadline()
44 elif clickbaitType == 3:
45 headline = generateBigCompaniesHateHerHeadline()
46 elif clickbaitType == 4:
47 headline = generateYouWontBelieveHeadline()
48 elif clickbaitType == 5:
49 headline = generateDontWantYouToKnowHeadline()
50 elif clickbaitType == 6:
51 headline = generateGiftIdeaHeadline()
52 elif clickbaitType == 7:
53 headline = generateReasonsWhyHeadline()
54 elif clickbaitType == 8:
55 headline = generateJobAutomatedHeadline()
56
57 print(headline)
58 print()
59
60 website = random.choice(['wobsite', 'blag', 'Facebuuk', 'Googles',
61 'Facesbook', 'Tweedie', 'Pastagram'])
62 when = random.choice(WHEN).lower()
63 print('Post these to our', website, when, 'or you\'re fired!')
64
65
66# Each of these functions returns a different type of headline:
67def generateAreMillenialsKillingHeadline():
68 noun = random.choice(NOUNS)
69 return 'Are Millenials Killing the {} Industry?'.format(noun)
70
71
72def generateWhatYouDontKnowHeadline():
73 noun = random.choice(NOUNS)
74 pluralNoun = random.choice(NOUNS) + 's'
75 when = random.choice(WHEN)
76 return 'Without This {}, {} Could Kill You {}'.format(noun, pluralNoun, when)
77
78
79def generateBigCompaniesHateHerHeadline():
80 pronoun = random.choice(OBJECT_PRONOUNS)
81 state = random.choice(STATES)
82 noun1 = random.choice(NOUNS)
83 noun2 = random.choice(NOUNS)
84 return 'Big Companies Hate {}! See How This {} {} Invented a Cheaper {}'.format(pronoun, state, noun1, noun2)
85
86
87def generateYouWontBelieveHeadline():
88 state = random.choice(STATES)
89 noun = random.choice(NOUNS)
90 pronoun = random.choice(POSSESIVE_PRONOUNS)
91 place = random.choice(PLACES)
92 return 'You Won\'t Believe What This {} {} Found in {} {}'.format(state, noun, pronoun, place)
93
94
95def generateDontWantYouToKnowHeadline():
96 pluralNoun1 = random.choice(NOUNS) + 's'
97 pluralNoun2 = random.choice(NOUNS) + 's'
98 return 'What {} Don\'t Want You To Know About {}'.format(pluralNoun1, pluralNoun2)
99
100
101def generateGiftIdeaHeadline():
102 number = random.randint(7, 15)
103 noun = random.choice(NOUNS)
104 state = random.choice(STATES)
105 return '{} Gift Ideas to Give Your {} From {}'.format(number, noun, state)
106
107
108def generateReasonsWhyHeadline():
109 number1 = random.randint(3, 19)
110 pluralNoun = random.choice(NOUNS) + 's'
111 # number2 should be no larger than number1:
112 number2 = random.randint(1, number1)
113 return '{} Reasons Why {} Are More Interesting Than You Think (Number {} Will Surprise You!)'.format(number1, pluralNoun, number2)
114
115
116def generateJobAutomatedHeadline():
117 state = random.choice(STATES)
118 noun = random.choice(NOUNS)
119
120 i = random.randint(0, 2)
121 pronoun1 = POSSESIVE_PRONOUNS[i]
122 pronoun2 = PERSONAL_PRONOUNS[i]
123 if pronoun1 == 'Their':
124 return 'This {} {} Didn\'t Think Robots Would Take {} Job. {} Were Wrong.'.format(state, noun, pronoun1, pronoun2)
125 else:
126 return 'This {} {} Didn\'t Think Robots Would Take {} Job. {} Was Wrong.'.format(state, noun, pronoun1, pronoun2)
127
128
129# If the program is run (instead of imported), run the game:
130if __name__ == '__main__':
131 main()