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

#1 AntiEinstein   User is offline

  • New D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 10
  • Joined: 19-August 15

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."

Is This A Good Question/Topic? 0
  • +

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

#2 Ryano121   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1461
  • View blog
  • Posts: 3,289
  • Joined: 30-January 11

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

Posted 03 September 2015 - 08:00 AM

Hackish way of doing it without the dictionary: :)/>

def finder(letter):
    return ord(letter) - 96


(will not work correctly for uppercase)

This post has been edited by Ryano121: 03 September 2015 - 08:01 AM

Was This Post Helpful? 0
  • +
  • -

#3 whoisit   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 15-October 08

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

Posted 09 January 2016 - 09:56 AM

Tried your snippet in Python 3.4 but it didn't work.
Ammended it to this.
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 ("You picked the letter \'" + letter + "\' which is number \'" + str(alphabet_number[letter]) + "\' in the alphabet.")

        else:

            print (letter + " Is not in the alphabet.") #Show what they pressed wrong
       
letter = input("Type a letter.")
        
finder(letter.lower()) #In case they have caps lock switched on!




Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6813
  • View blog
  • Posts: 28,186
  • Joined: 12-December 12

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.

Print Is A Function
Was This Post Helpful? 0
  • +
  • -

#5 whoisit   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 15-October 08

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

Posted 10 January 2016 - 02:32 AM

Andrew thanks for the info.
Anti thanks for the snippet, below is what I finally ended up with.

1) User can keep entering letters as long as they want.
2) Shows user what they entered wrong if they don't type a letter.
3) Lets them type quit to exit program.
4) Allows them to type capitals.

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}
print ("""\t\t\tFinding the position of letters in the alphabet.
\t\t\t\tType \'quit\' to end program\n""")
 
def finder(letter): #function
        
        if letter == ("quit"):
            print ("Goodbye")
            quit()
        elif letter.isalpha()==True and len(letter)==1:
            print ("\nThe letter \'" + letter.upper() + "\' or \'" + letter + "\' is number \'" + str(alphabet_number[letter]) + "\' in the alphabet.")
        else:
            print ("\n" + letter + " Is not in the alphabet.") #Show what they pressed wrong
#main
letter = ""

while letter != "quit":
    letter = input("Type a letter to find it's position in the alphabet.")     
    finder(letter.lower()) #In case they have caps lock switched on!




Was This Post Helpful? 0
  • +
  • -

Page 1 of 1