python面試題

來源:互聯網
上載者:User
# name, age, scoretom, 12, 86Lee, 15, 99Lucy, 11, 58Joseph, 19, 56

第一欄為姓名(name),第二欄為年紀(age),第三欄為得分(score)

現在,寫一個Python程式,

1)讀取檔案

2)列印如下結果:

得分低於60的人都有誰?

誰的名字以L開頭?

所有人的總分是多少?

3)姓名的首字母需要大寫,該record.txt是否符合此要求? 如何糾正錯誤的地方?

#read lines from filefobj = open('record.txt', 'r+')print 'opened file: ', fobj.nameall_lines = fobj.readlines()fobj.close()lines = [l[:-1].split(', ') for l in all_lines if not l.startswith('#') and l.strip()]#list person who's score less than 60print [s[0] for s in lines if int(s[2]) < 60]#list person who's name starts with 'L'print [s[0] for s in lines if s[0].startswith('L')]#compute the score of all personprint sum([int(s[2]) for s in lines])#write new lines contains capitalize name into filefobj = open('record2.txt', 'w+')print 'opend file: ', fobj.namenewlines = []for line in all_lines:    if line[0].islower():        line = line.capitalize()    newlines.append(line)print newlinesif newlines:    fobj.writelines(newlines)fobj.close()
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.