Demo entry 6363225

Challenge 7 - LetterList

   

Submitted by anonymous on May 12, 2017 at 16:11
Language: Python 3. Code size: 1.9 kB.

from tkinter import *
import tkinter.filedialog as msgbox

def containsOnlyLetters(a):
    a = list(a)

    alphabet = list("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")
    
    for n in range(len(a)):
        if not (a[n] in alphabet):
            return False
            break
    return True
    

root = Tk()
root.withdraw()

letter = input("Please enter a letter: ")
data = []

while (len(letter) != 1) or not containsOnlyLetters(letter):
    letter = input("Must be 1 character long, and contain only letters (A-Z and a-z).\nPlease enter a letter: ")

try:
    file = {"y":True,"n":False}[input("Use words from file? (y/n) ").lower()]
except KeyError:
    print("Input not recognised. Quitting...")
    raise SystemExit

if file:
    try:
        fileOptions = [("Text Files (*.txt)", "*.txt"),("All files","*")]
        data = open(msgbox.askopenfilename(filetypes = fileOptions),'r').read()
    except FileNotFoundError:
        print("The user cancelled the operation.\nQuitting...")
        raise SystemExit

    
    data = data.split("\n")
else:
    inputData = input("Enter a string to add to the list or type -stop finish and search the list: ")

    while inputData != "-stop":
        if containsOnlyLetters(inputData) and len(inputData) > 0:
            data.append(inputData)
            inputData = input("Enter a string to add to the list or type -stop finish and search the list: ")
        else:
            print("Must Contain Only letters, and connot be blank.")
            inputData = input("Enter a string to add to the list or type -stop finish and search the list: ")

output = []

for n in data:
    if n[0].upper() == letter.upper():
        output.append(n)

print("\nFound",len(output),"words starting with \""+letter+"\".","\nThey are:\n"+"\n\n".join(output))

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).