f=open(“F:\Desktop\今日须看\代码\Python-master\AboutFile.txt”)
fileContent=""
while True:
line=f.readline()
if line=="":
break
fileContent+=line
f.close()
print(fileContent)
import random
list=[1,2,3,4,5,6,7,8,9,0]
slice=random.sample(list,5) # 随机从列表中选出5个数字
print(slice)
print(list)
import re
test=‘用户输入的字符串’
if re.match(r’正则表达式’,test):
print(‘ok’)
else:
print(‘fail’)
对着代码抄了一个程序,提升一下学习兴趣。
代码如下
import pygame
from pygame.locals import *
import sys
def play_tank():
pygame.init()
window_size=(width,height)=(600,600)
speed=[1,1]
color_black=(0,0,0)
screen=pygame.display.set_mode(window_size)
pygame.display.set_caption(‘自由移动坦克 & 翟海祥’)
# pygame.display.set_caption(‘翟海祥’)
tank_image=pygame.image.load(‘tank.bmp’)
tank_rect=tank_image.get_rect()
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.qiut()
sys.exit()
tank_rect=tank_rect.move(speed)
if (tank_rect.left<0) or (tank_rect.right>width):
speed[0]=-speed[0]
if (tank_rect.top<0) or (tank_rect.bottom>height):
speed[1]=-speed[1]
screen.fill(color_black)
screen.blit(tank_image,tank_rect)
pygame.display.update()
if name==‘main’:
play_tank()