[1] code:python
show
#!/usr/bin/env python3
import random
import time
import sys
def clear_screen():
'''Clear the screen with a simple approach that works across platforms.'''
print('\n' * 100)
def print_with_delay(text, delay=0.03):
'''Print text with a slight delay between characters for better UX.'''
for char in text:
sys.stdout.write(char)
sys.stdout.flush()
time.sleep(delay)
print()
class JokeGenerator:
'''A class to manage and display jokes for teens.'''
def __init__(self):
# List of teen-appropriate jokes
self.jokes = [
“Why don't scientists trust atoms? Because they make up everything!”,
“What did the ocean say to the beach? Nothing, it just waved!”,
“I told my computer I needed a break, and now it won't stop sending me vacation ads.”,
“What do you call a fake noodle? An impasta!”,
“Why did the math book look sad? Because it had too many problems.”,
“How does a penguin build its house? Igloos it together!”,
“What do you call a bear with no teeth? A gummy bear!”,
“Why don't skeletons fight each other? They don't have the guts!”,
“Did you hear about the mathematician who's afraid of negative numbers? He'll stop at nothing to avoid them!”,
“What's the best thing about Switzerland? I don't know, but the flag is a big plus!”,
“Why did the bicycle fall over? Because it was two-tired!”,
“What did one wall say to the other wall? I'll meet you at the corner!”,
“How do you organize a space party? You planet!”,
“Why couldn't the leopard play hide and seek? Because he was always spotted!”,
“What do you call a dinosaur with an extensive vocabulary? A thesaurus!”,
]
# Keep track of recently told jokes to avoid repetition
self.recent_jokes = []
def get_joke(self):
'''Return a random joke that hasn't been used recently.'''
available_jokes = [joke for joke in self.jokes if joke not in self.recent_jokes]
# If we've used all jokes, reset the recent list
if not available_jokes:
self.recent_jokes = []
available_jokes = self.jokes
# Select a random joke
joke = random.choice(available_jokes)
# Add to recent jokes, keeping only the last 5
self.recent_jokes.append(joke)
if len(self.recent_jokes) > 5:
self.recent_jokes.pop(0)
return joke
def main():
'''Main function to run the joke program.'''
joke_gen = JokeGenerator()
clear_screen()
print_with_delay('=== Teen Joke Generator ===')
print_with_delay('Get ready to laugh with some awesome jokes!')
print()
while True:
print('What would you like to do?')
print('1. Hear a joke')
print('2. Hear 3 jokes in a row')
print('3. Exit')
choice = input('\nEnter your choice (1-3): ').strip()
if choice == '1':
clear_screen()
print_with_delay('Here's a joke for you:')
print()
print_with_delay(joke_gen.get_joke())
print()
input('Press Enter to continue…')
clear_screen()
elif choice == '2':
clear_screen()
print_with_delay('Here are 3 jokes coming right up!')
print()
for i in range(3):
print_with_delay(f'Joke #{i+1}:')
print_with_delay(joke_gen.get_joke())
print()
if i < 2: # Don't pause after the last joke
time.sleep(1)
input('Press Enter to continue…')
clear_screen()
elif choice == '3':
print_with_delay('Thanks for laughing with us! Goodbye!')
sys.exit(0)
else:
print('Invalid choice. Please choose 1, 2, or 3.')
time.sleep(1)
clear_screen()
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
print('\nGoodbye!')
sys.exit(0)