Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import os
- import random
- ALPHABET = "abcdefghijklmnopqrstuvwxyz"
- # checks if something is a pangram (case-insensitively) by checking if it contains all characters in the alphabet
- def is_pangram(string):
- string = string.lower()
- return all(char in string for char in ALPHABET)
- PANGRAMS_FILE = "pangrams.txt"
- def check_pangrams():
- while True:
- string = input("Input a potential pangram (enter e to see examples): ")
- if string == "e":
- example_pangrams()
- else:
- pangram = is_pangram(string)
- print(pangram)
- already_seen = False
- # ensure pangram uniqueness by checking existing pangrams in file
- if os.path.exists(PANGRAMS_FILE):
- with open(PANGRAMS_FILE, "r") as file:
- already_seen = string in file.read()
- # if the pangram is not already stored, write it to a file
- if not already_seen and pangram:
- with open(PANGRAMS_FILE, "a") as file:
- file.write(string + "\n")
- def example_pangrams():
- with open(PANGRAMS_FILE, "r") as file:
- pangrams = [ line.strip() for line in file.readlines() ]
- random.shuffle(pangrams)
- pangram_count = int(input("How many pangrams? "))
- if pangram_count > len(pangrams):
- print(f"Not enough pangrams available (maximum is {len(pangrams)}).")
- else:
- print("\n".join(pangrams[:pangram_count]))
- check_pangrams()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement