PAT-B 1002. 写出这个数 (python)

1. dictionary初始化  dic = {}

2. list初始化 ans = []

3. list遍历时两种方法

3.1 从前往后

    for i in range(len(ans)):
        print dic[ans[i]],
3.2 从后往前?

    for i in ans
        print dic[ans[i]],
4. python输出不换行: print "", 

    注意到,此时在不同的print字符串间会自动输出一个空格的

源码:

if __name__ == "__main__":
    cnt = 0
    a = input()
    while a != 0:
        cnt = cnt + a % 10
        a = a / 10    
    ans = []
    while cnt != 0:
    	ans.insert(0, cnt % 10)
    	cnt = cnt / 10
    dic = {}
    dic[0] = "ling"
    dic[1] = "yi"
    dic[2] = "er"
    dic[3] = "san"
    dic[4] = "si"
    dic[5] = "wu"
    dic[6] = "liu"
    dic[7] = "qi"
    dic[8] = "ba"
    dic[9] = "jiu"
#    print ans
    for i in range(len(ans)):
        print dic[ans[i]],


你可能感兴趣的:(PAT-B 1002. 写出这个数 (python))