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()