+ -
当前位置:首页 → 问答吧 → 用python计算人生的成功因素~请记住这题,这不止是巧合

用python计算人生的成功因素~请记住这题,这不止是巧合

时间:2010-12-18

来源:互联网

上网的时候看到一道题挺有意思的就用python写下来了。

  把a~z分别对应值1~26,把输入的单词的每个字母的值加到一起就是结果。

Python code

# -*- coding: utf-8 -*-   
  
def live():   
    dict1 = dict(zip([chr(i) for i in range(97, 123)],    
        [i for i in range(1, 27)]))   
    print ''''' ‘~’ 退出'''  
    while True:   
        str = raw_input('Give me a word: ')   
        if str == '~':   
            break  
        str = str.split()           
        sun1 = 0  
        for i in range(len(str)):   
            try:   
                for wd in str[i]:   
                    sun1 += dict1[(wd).lower()]                   
            except Exception, e:   
                print '\nWrong ! Must be a word.\n', e   
        print sun1   
if __name__ == '__main__':   
    live()   
  
  
>>>~’ 退出   
Give me a word: Hard work   
98  
Give me a word: Knowledge   
96  
Give me a word: love   
54  
Give me a word: luck   
47  
Give me a word: Money   
72  
Give me a word: Leadership   
97  
Give me a word: ATTITUDE   
100  
Give me a word: ~   
  
>>> ATTITUDE 态度决定一切- -!  

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/a590687/archive/2010/12/18/6083461.aspx

作者: a590687   发布时间: 2010-12-18

hehe

Python code

>>> sum([ord(c.lower()) - 97 + 1 for c in s if ord(c.lower()) in range(97, 123)])
98
>>> f = lambda s: sum([ord(c.lower()) - 97 + 1 for c in s if ord(c.lower()) in range(97, 123)])
>>> f('Hard Work')
98
>>> f('love')
54
>>> f('ATTITUDE')
100
>>> f('Wrong Spelling for Sum')
263

作者: notax   发布时间: 2010-12-18