python入门之小栗子
1 点球小游戏:
from random import choice
score=[0,0]
direction=['left','center','right']
def kick():
print '=====You Kick!====='
print 'Choose one side to shoot'
print 'left center right'
you=input()
print 'You kicked '+you
com=choice(direction)
print 'Computer saved '+com
if you!=com:
print 'Goal!'
score[0] += 1
else:
print 'Oops...'
print 'Score:%d(you) - %d(com)\n'%(score[0],score[1])
print '=====You Save====='
print 'Choose one side to save!'
print 'left center right'
you=input()
print 'You saved '+you
com=choice(direction)
print 'Computer kickde '+com
if you==com:
print 'Saved!'
else:
print 'Oops...'
score[1]+=1
print 'score:%d(you) - %d(com)\n'%(score[0],score[1])
for i in range(5):
print '====== Round %d ======='%(i+1)
kick()
while(score[0]==score[1]):
i+=1
print '====== Round %d ======='%(i+1)
kick()
if score[0]>score[1]:
print 'You Win!'
else:
print 'You lose.'
2,处理文件中的数据(学生成绩的汇总):
f=file('data.txt')
lines=f.readlines()
print lines
results=[]
for line in lines:
data=line.split()
sum=0
for score in data[1:]:
sum+=int(score)
result='%s\t:%d\n'%(data[0],sum)
results.append(result)
output=file('result.txt','w')
output.writelines(results)
output.close()
3(#),抓取百度首页的代码 保存成html文件,
4,