4 Replies - 11434 Views - Last Post: 10 January 2016 - 02:32 AM

Using a dictionary to show the rank of any letter.

Posted 21 August 2015 - 03:32 PM

alphabet_number={"a":1,"b":2,"c":3,"d":4,"e":5,"f":6,"g":7,"h":8,"i":9,"j":10,"k":11,"l":12,"m":13,"n":14,"o":15,"p":16,"q":17,"r":18,"s":19,"t":20,"u":21,"v":22,"w":23,"x":24,"y":25,"z":26}
def finder(letter):
if letter.isalpha()==True and len(letter)==1:
print "Your letter is the %s. letter of the alphabet."%(alphabet_number[letter])
else:
print "It is an invalid response to the question."
letter=raw_input("Write your letter.")
finder(letter)

First I made a dictionary and as the key I wrote my numbers, then as the value I wrote their rank which will be found at the end of the program. After that I created a function which contain if. As you see if will only work if user wrote a letter. Anything else will simply lead to the print command "It is an invalid response to the question."

Re: Using a dictionary to show the rank of any letter.

Posted 09 January 2016 - 11:22 AM

To clarify, that's because in Python 3 print is a function print("something") rather than a statement print "something". Parentheses can be applied in both Python 2 and 3 to ease the transition between the two.