Jun-19-2017, 10:46 PM
I have setup the class methods I would like to use instead of the code below the class. I am new to classes and still learning, how to make them work effectively.
Any help or tips would be greatly appreciated!
The code should print the numerical value of the letter according to its location in the alphabet, from the stored dictionary. The text file read in is just the alphabet a-z.
Any help or tips would be greatly appreciated!
The code should print the numerical value of the letter according to its location in the alphabet, from the stored dictionary. The text file read in is just the alphabet a-z.
import numpy as np import string import collections.abc class Alphabet(collections.abc.Mapping): def __len__(self): pass def __iter__(self): pass def __getitem__(self,key): pass filename= "alphabet.txt" # Assigns the file to a variable alphabet= np.genfromtxt(filename, dtype= str) # reads in alphabet from file my_dict= dict.fromkeys(alphabet,0) # Sets the imported Alphabet letters to keys i=0 # Initialize the incrementation alphabet_loc= [] # Initialize the alphabets value storage for i in range(len(alphabet)): # Iterates as many times as there are characters alph_value= ord(alphabet[i])-96 # finds the ordinal value and minus 96 to get to 1 alphabet_loc.append(alph_value) # Fills the value array my_dict[alphabet[i]]= alphabet_loc[i] # Builds the dictionary Key values def get_key(): # Function for getting the key from the user. input_char=input("Please enter a single letter of the alphabet: ").lower() # User input, converts in lowercase allowed_char= string.ascii_letters # used to verify the user input is a letter while(len(input_char) != 1 or input_char not in allowed_char): # Loop for checking length and character entered input_char= input("Please enter a single letter of the alphabet: ").lower() # user input, converts in lowercase return input_char # Ends the while loop and return the value key_char= get_key() # Calls on the function and receives the user input Key print(my_dict[key_char]) # Print out the the ordinal value for the user inputed KeyHelp