Advertisement
osmarks

Untitled

Feb 22nd, 2021
672
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.50 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. import os
  4. import random
  5.  
  6. ALPHABET = "abcdefghijklmnopqrstuvwxyz"
  7.  
  8. # checks if something is a pangram (case-insensitively) by checking if it contains all characters in the alphabet
  9. def is_pangram(string):
  10.     string = string.lower()
  11.     return all(char in string for char in ALPHABET)
  12.  
  13. PANGRAMS_FILE = "pangrams.txt"
  14.  
  15. def check_pangrams():
  16.     while True:
  17.         string = input("Input a potential pangram (enter e to see examples): ")
  18.         if string == "e":
  19.             example_pangrams()
  20.         else:
  21.             pangram = is_pangram(string)
  22.             print(pangram)
  23.             already_seen = False
  24.             # ensure pangram uniqueness by checking existing pangrams in file
  25.             if os.path.exists(PANGRAMS_FILE):
  26.                 with open(PANGRAMS_FILE, "r") as file:
  27.                     already_seen = string in file.read()
  28.             # if the pangram is not already stored, write it to a file
  29.             if not already_seen and pangram:
  30.                 with open(PANGRAMS_FILE, "a") as file:
  31.                     file.write(string + "\n")
  32.  
  33. def example_pangrams():
  34.     with open(PANGRAMS_FILE, "r") as file:
  35.         pangrams = [ line.strip() for line in file.readlines() ]
  36.     random.shuffle(pangrams)
  37.     pangram_count = int(input("How many pangrams? "))
  38.     if pangram_count > len(pangrams):
  39.         print(f"Not enough pangrams available (maximum is {len(pangrams)}).")
  40.     else:
  41.         print("\n".join(pangrams[:pangram_count]))
  42.  
  43. check_pangrams()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement