Python Function That Receives Letter, Returns (0-B

2019-07-28 09:51发布

I'm trying to create a Python function that receives a letter (a string with only one alphabetic character) and returns the 0-based numerical position of that letter in the alphabet. It should not be case-sensitive, and I can't use import.

So entering "a" should return

0

Entering "A" should also return

0

Entering "O" should return

14

And so on.

I had noticed this question but the top answer uses import and the second answer doesn't make any sense to me / doesn't work. I tried to apply the second answer like this:

letter = input("enter a letter")

def alphabet_position(letter):

    return ord(letter) - 97

print((alphabet_position)(letter))

but I got a TypeError:

TypeError: ord() expected a character, but string of length 2 found

Just like the asker in the question that I linked, I'm also trying to send the characters "x" amount of steps back in the alphabet, but in order to do that I need to create this helper function first.

I'm thinking there must be a way to store the letters in two separate lists, one lower-case and one upper-case, and then see if the string that the user entered matches one of the items in that list? Then once we find the match, we return it's (0-based) numerical position?

letter = input("enter a letter")

def alphabet_position(letter):

    position = 0
    #letter_position = index value that matches input
    lower_case_list ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
 'y', 'z'] 

    upper_case_list ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
 'Y','Z']              

#if letter is in lower_case_list, return it's 0-based numerical position.
#else, if letter is in upper_case_list, return it's 0-based numerical position.
#else, print("Enter a valid letter")

return letter_position

Please help if you have any suggestions. Thank you.

3条回答
Lonely孤独者°
2楼-- · 2019-07-28 10:27

It's probably simpler to just convert from uppercase or lowercase to specifically lowercase with the .lower() method, and use the built in list of letters (string.ascii_lowercase). You can find the index of a list's element by using the .index() method.

import string

letter = input('enter a letter: ')

def alphabet_position(letter):
    letter = letter.lower()
    return list(string.ascii_lowercase).index(letter)


print(alphabet_position(letter))

When you called alphabet_position, it is expecting an argument so you need to do func_name(arg) format.

查看更多
狗以群分
3楼-- · 2019-07-28 10:34

Another way you could do this is to use dictionary comprehension to create a dict of letter-position pairs, like so:

from string import lowercase as l

alphabet_lookup = {letter:pos for letter,pos in zip(l, range(len(l)))}

and then

f = lambda letter: alphabet_lookup[letter.lower()]

is the desired function.

查看更多
甜甜的少女心
4楼-- · 2019-07-28 10:40

I suggest using a dictionary. It might be a large amount of code to do something relatively simple, but I find it easier to make sense of it this way (and if you are new to python it will help you learn). If you google python dictionaries you can learn lots more about them, but here is the basic concept:

Code for python version 3.X:

def alphabet_position(letter):
    alphabet_pos = {'A':0, 'a':0, 'B':1, 'b':1}
    pos = alphabet_pos[letter]
    return pos
letter = input('Enter a letter: ')
print(alphabet_position(letter))

Code for python version 2.7:

def alphabet_position(letter):
    alphabet_pos = {'A':0, 'a':0, 'B':1, 'b':1}
    pos = alphabet_pos[letter]
    return pos
letter = raw_input('Enter a letter: ')
print alphabet_position(letter)

If you run that, it will print 1 because it searches through the alpahbet_pos dictionary and finds the value that corresponds to the entry entitled 'B'. Notice that you can have multiple entries with the same value, so you can do uppercase and lowercase in the same dictionary. I only did letters A and B for the sake of time, so you can fill out the rest yourself ;)

I once had to enter every element on the periodic table and their corresponding atomic mass, and that took forever (felt much longer than it actually was).

查看更多
登录 后发表回答