2017-09-01 50 views

回答

1

如果letter是可變的,你可以做這樣的事情:

number = ord(letter) - ord('a') + 1 

letter只能取值 'A', 'b',..., 'Z'。

+1

'-26'沒有用。 'ord(letter) - 26'應該給你什麼號碼? – khelwood

+0

對不起,這是一個愚蠢的錯誤。我想寫96,這是'a'字符的ASCII碼減1。 – GLR

+1

可能不是使用幻數,而是使用'ord(letter) - ord('a')'。 – khelwood

0
def convert(sentence): 
    output = "" 
    for letter in sentence: 
     alphaNum = ord(letter.lower()) - 96 
     output += str(alphaNum) + " " 
    return output 

呼叫convert("hello")會給你'8 5 12 12 15 ' 你可以調整輸出格式一點點,不知道這是你想要的嗎?

相關問題