2015-11-02 23 views
-1

我需要帮助将此代码转换为小写字母,然后子字符串删除空格然后找到ASCII值然后将它们加起来得到一笔总和,这就是我写的:需要帮助将字符串转换为Unicode然后添加值

def main(): 
    # Handshake 
    print("This program computes a total value for a user's full name") 

    # Prompt and read input 
    fullname = input("Please enter your full name: ") 

    # Convert user's full name to all lower case letters 
    fullname = fullname.lower() 

    # Split user's full name into substrings 
    fullname = fullname.split() 

    # Loop through each substring and compute 
    # total ASCII value of user's name 
    totalvalue = 0 
    for character in fullname: 
     fullname = ord(character) + totalvalue 

    #Display outputs 
    print("\nYour name is: ", fullname) 
    print("\nThe value of your name is: ", totalvalue) 

main() 
+1

找不到问号! – manetsus

回答

0

两个问题:(1)分开后,全称是现在的名单,所以你需要第二个循环遍历字符,和(2)你在循环总结时输入fullname而不是totalvalue。试着用替换循环:

totalvalue = 0 
for name in fullname: 
    for character in name: 
     totalvalue += ord(character) 
+0

因此在分割它们之后为角色写第二个循环? – Tin2185

+0

感谢您的帮助 – Tin2185

0

什么这样的事情,你用一个列表理解:

import sys 

def main(): 
    # Prompt and read input 
    text= raw_input().decode(sys.stdin.encoding) 
    values = [ord(c) for c in text] 
    print(values) 
main() 

其中值是字符串的ASCII值的列表,你可以PROB做从那里休息

+0

感谢您的帮助 – Tin2185

相关问题