python作业9

陈皓:没写完~

苗子瑾:第五个没写完
陈皓:第六个
陈皓:一遇到数学就蒙
苗子瑾:想想就头疼
苗子瑾:等我回宿舍交作业吧……
周愉:看见那个绿色的灯了么
周愉:全灭了我就到家了
陈皓:我家网络出门题了
陈皓:刚给联通打完电话,效率问题~
陈皓:效率问题
苗子瑾:这个图打不开
苗子瑾:这个图

1.有以上文件record.txt,将此文件三人对话每个人的内容单独保存一个文件,并每个文件中不包含对话人名

def threem():
    with open('record.txt', encoding='utf-8') as rf:
        while True:
            data = rf.readline()
            if data == '':
                break
            for i in data:
                if i == ':':
                    if data[0:data.index(i)] == '陈皓':
                        with open('w1', mode='a+', encoding='utf-8') as wf:
                            wf.write(data[data.index(i)+1:])
                    elif data[0:data.index(i)] == '苗子瑾':
                        with open('w2', mode='a+', encoding='utf-8') as wf:
                            wf.write(data[data.index(i)+1:])
                    elif data[0:data.index(i)] == '周愉':
                        with open('w3', mode='a+', encoding='utf-8') as wf:
                            wf.write(data[data.index(i)+1:])
                    else:
                        pass

threem()

 

 

2.读入用户输入的文件的路径和一个字符串和行数,将文件中的第n行行首插入用户输入的字符串

def mdir():
    s = input('请输入文件名:')
    n = int(input('请输入行数:'))
    s1 = input('请输入字符串:')
    with open(s, mode= 'r+', encoding='utf - 8') as fd:
        fd.seek(0, 0)
        for i in range(n - 1):
            fd.readline()
        a = fd.tell()
        text1 = fd.readlines()
        text1 = ''.join(text1)
        print(text1)
        fd.seek(a)
        fd.write(s1)
        fd.seek(0, 1)
        fd.write(text1)

mdir()

 

3.下面只有一种方式不能打开文件,请尝试,并说明原因?

  01.  f = open('E:/test.txt', 'w')

  02.  f = open('E:\test.txt', 'w')

  03.  f = open('E://test.txt', 'w')

  04.  f = open('E:\\test.txt', 'w')

一个反斜杠对于python中有转义符的作用,比如\t,\n,所以报错,双反斜杠就是解决这个问题,或者在路径前加上’r’避免这种问题

4.打开一个文件使用open()函数的时候,通过设置文件的打开方式,决定打开的文件具有哪些性质,请总结都有哪些方式,并说明区别

r只读,r+读写,不创建

w新建只写,w+新建读写,二者都会将文件内容清零

(以w方式打开,不能读出。w+可读写)

w+与r+区别:

r+:可读可写,若文件不存在,报错;w+: 可读可写,若文件不存在,创建

以a,a+的方式打开文件,附加方式打开

a:附加写方式打开,不可读;a+: 附加读写方式打开

b是二进制文件,r+b就是以二进制读写模式打开

5.如何将一个文件对象f中的数据存放到列表中

with open('f', encoding='utf-8') as rf:
    data = [rf.readlines()]
    print(data)

6.如果得到文件对象f的每一行数据,尝试使用多种方法

n = int(input('请输入第几行:'))

with open('f') as fd:
    fd.seek(0, 0)
    s = fd.readlines()
    print(s[n-1])

附加:发牌

from tkinter import *
from PIL import Image, ImageTk
import random
import time

root = Tk()
root.geometry('1400x1000')
root.title('xxxx')
root.resizable(0, 0)

class Card(object):
    def __init__(self, card=[]):
        self.card = card

    def createCard(self):
        s = '.jpg'
        for i in range(1,53):
            s1 = str(i)
            self.card += [''.join((s1,s))]

    def randomCard(self):
        random.shuffle(self.card)

    def deal(self, play):
        n = 0
        m = 0
        for i in range(52):
            play.player[m].card.append(self.card[i])
            n += 1
            if n == 13:
                n = 0
                print(play.player[m].card)
                print(id(play.player[m]))
                print(id(play.player[m].card))
                m += 1


class Player(object):
    def __init__(self, card=[]):
        self.card = card

    def players(self, player=[]):
        self.player = ['高进','刀仔','阿星','达叔']
        for i in range(4):
            self.player[i] = Player()
            self.player[i].card = []


def start():
    if v.get() == '发牌':
        v.set('重新发牌')

    card = Card()
    card.createCard()
    card.randomCard()
    play = Player()
    play.players()
    card.deal(play)


    n = 0
    m = 0
    t = 0
    img = []
    photo = []
    imglabe = []
    for i in range(0, 52):
        t += 50
        img += ['x']
        photo += ['y']
        imglabe += ['z']
        img[i] = Image.open(play.player[n].card[m])
        photo[i] = ImageTk.PhotoImage(img[i])
        imglabe[i] = Label(root, image=photo[i])
        time.sleep(0.2)
        if n == 0:
            imglabe[i].place(x=250 + t, y=50)
        elif n == 1:
            imglabe[i].place(x=250 + t, y=750)
        elif n == 2:
            imglabe[i].place(x=100, y=50 + t)
        elif n == 3:
            imglabe[i].place(x=1100, y=50 + t)
        else:
            pass
        imglabe[i].update()
        m += 1
        if m ==13:
            n += 1
            m = 0
            t = 0
    root.mainloop()

btn1 = Button(root, height=2, width=20, font=20)
btn1.place(x=600, y=450)
btn1['command'] = start

v = StringVar()
v.set('发牌')
btn1['textvariable'] = v


root.mainloop()

8.阅读下面的代码,它的输出结果是什么?

class A(object):
    def go(self):
        print "go A go!"
    def stop(self):
        print "stop A stop!"
    def pause(self):
        raise Exception("Not Implemented")
class B(A):
    def go(self):
        super(B, self).go()
        print "go B go!"
class C(A):
    def go(self):
        super(C, self).go()
        print "go C go!"
    def stop(self):
        super(C, self).stop()
        print "stop C stop!"
class D(B,C):
    def go(self):
        super(D, self).go()
        print "go D go!"
    def stop(self):
        super(D, self).stop()
        print "stop D stop!"
    def pause(self):
        print "wait D wait!"
class E(B,C): pass
a = A()
b = B()
c = C()
d = D()
e = E()
# 说明下列代码的输出结果
a.go()
b.go()
c.go()
d.go()
e.go()
a.stop()
b.stop()
c.stop()
d.stop()
e.stop()
a.pause()
b.pause()
c.pause()
d.pause()
e.pause()
#go A go!

#go A go!
#go B go!

#go A go!
#go C go!

#go A go!
#go C go!
#go B go!
#go D go!

#go A go!
#go C go!
#go B go!

#stop A stop!

#stop A stop!

#stop A stop!
#stop C stop!

#stop A stop!
#stop C stop!
#stop D stop!

#stop A stop!
#stop C stop!

#异常

#异常

#异常

#wait D wait!

#异常

坦克

某次战役中,为便于信息交互,我军侦察部门将此次战役的关键高地坐标设定为(x=0,y=0)并规定,每向东增加100米,x加1,每向北增加100米,y加1。同时,我军情报部门也破译了敌军向坦克发送的指挥信号,其中有三种信号(L,R,M)用于控制坦克的运动,L 和 R 分别表示使令坦克向左、向右转向,M 表示令坦克直线开进100米,其它信号如T用于时间同步,P用于位置较准。

一日,我军侦察兵发现了敌军的一辆坦克,侦察兵立即将坦克所在坐标(P, Q)及坦克前进方向(W:西,E:东,N:北,S:南)发送给指挥部,同时启动信号接收器,将坦克接收到的信号实时同步发往指挥部,指挥部根据这些信息得以实时掌控了该坦克的位置,并使用榴弹炮精准地击毁了该坦克。

请设计合理的数据结构和算法,根据坦克接收到的信号,推断出坦克所在的位置。
设计时请考虑可能的扩展情况,并体现出您的设计风格。

假设,侦察兵发送给指挥部的信息如下:
坦克坐标:(11,39)
坦克运行方向:W
坦克接收到的信号为:MTMPRPMTMLMRPRMTPLMMTLMRRMP
其位置应该是(9,43),运动方向为E

class Tank(object):
    def __init__(self, xlobal, ylobal, direction):
        self.xlobal = xlobal
        self.ylobal = ylobal
        self.direction = direction


    def count(self, signal):

        for i in signal:
            if i == 'T' or i == 'P':
                pass
            elif i == 'M' and self.direction == 'W':
                self.xlobal -= 1
            elif i == 'M' and self.direction == 'E':
                self.xlobal += 1
            elif i == 'M' and self.direction == 'N':
                self.ylobal += 1
            elif i == 'M' and self.direction == 'S':
                self.ylobal -+ 1

            elif i == 'L' and self.direction == 'W':
                self.direction = 'S'

            elif i == 'L' and self.direction == 'S':
                self.direction = 'E'

            elif i == 'L' and self.direction == 'E':
                self.direction = 'N'

            elif i == 'L' and self.direction == 'N':
                self.direction = 'W'

            elif i == 'R' and self.direction == 'W':
                self.direction = 'N'

            elif i == 'R' and self.direction == 'N':
                self.direction = 'E'

            elif i == 'R' and self.direction == 'E':
                self.direction = 'S'

            elif i == 'R' and self.direction == 'S':
                self.direction = 'W'

            else:
                pass

    def res(self):
        print('目标位置为({},{}),运动方向为{}'.format(self.xlobal, self.ylobal, self.direction))


t1 = Tank(11,39,'W')
t1.count('MTMPRPMTMLMRPRMTPLMMTLMRRMP')
t1.res()

 

posted @ 2018-12-21 22:46  Tangyun966  阅读(211)  评论(0)    收藏  举报