一道python筆試題

2021-09-01 22:45:06 字數 1258 閱讀 6278

有乙個record.txt的文件,內容如下:

# name, age, score

tom, 12, 86

lee, 15, 99

lucy, 11, 58

joseph, 19, 56

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

現在,寫乙個python程式,

1)讀取檔案

2)列印如下結果:

得分低於60的人都有誰?

誰的名字以l開頭?

所有人的總分是多少?

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

#read lines from file

fobj = open('record.txt', 'r+')

print 'opened file: ', fobj.name

all_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 60

print [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 person

print sum([int(s[2]) for s in lines])

#write new lines contains capitalize name into file

fobj = open('record2.txt', 'w+')

print 'opend file: ', fobj.name

newlines =

for line in all_lines:

if line[0].islower():

line = line.capitalize()

print newlines

if newlines:

fobj.writelines(newlines)

fobj.close()

一道筆試題

看到一道筆試題,跟自己想的有點出入,就跑了下,看了看原因。我稍微改了下 include int main int argc,char argv 輸出結果 c 5 d 245 press any key to continue vc6.0 debug下的彙編 5 unsigned char a 0xa...

一道筆試題

上次去筆試的時候,有一道題,怎麼也沒做出來,當時也是很緊張,有些思路,但卻沒有做出來。有四個人要過乙個獨木橋,因為天比較黑,而且橋只能允許兩個人同時通過,並且他們只有乙個手電筒。四個人單獨同時橋的時間是1,2,5,8分鐘。問最短的時間是多少?當時我的答案 1和8,1回來,1 5,1回來,1 2 8 ...

一道筆試題

題目是這樣的 判斷乙個小於1000的正整數是否為素數。素數的定義就不說了,以下直接分析解法,畢竟是在寫與專業相關的東西,是給本專業的人看得,所以看的人應該有點基礎吧?求素數的問題是乙個數學上的難題,這是常識,但是本題目限制了最大範圍是在1000以內,所以就可以嘗試找出乙個足夠好的解了。首先給出乙個最...