2015-11-02 20 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

相關問題