提示:最好还是先思考,先编写,再看答案哦 ^_^
21.
import os.path
import shutil
def create_filefolder(folder_name):
if not os.path.exists(folder_name):
os.mkdir(folder_name)
def main():
create_filefolder("运行结果")
file_list = os.listdir('..\\images')
folders = []
for each in file_list:
folders.append(each.split('-')[0])
folders = set(folders)
os.chdir("运行结果")
for folder in folders:
create_filefolder(folder)
os.chdir('..\\..\\images')
for file in file_list:
shutil.move(file, '..\\py\\运行结果\\'+file.split('-')[0])
if __name__ == '__main__':
main()
22.
改进我们的小红球“”
class Ball:
def __init__(self,canvas,paddle,color):
self.canvas = canvas
self.paddle = paddle
self.id = canvas.create_oval(10,10,25,25,fill=color)
self.canvas.move(self.id,245,100)
starts = [-3,-2,-1,1,2,3]
random.shuffle(starts)
self.x = starts[0]
self.y = -3
self.canvas_height = self.canvas.winfo_height()
self.canvas_width = self.canvas.winfo_width()
def draw(self):
self.canvas.move(self.id,self.x,self.y)
pos = self.canvas.coords(self.id)
if pos[1] <= 0:
self.y = 3
if pos[3] >=self.canvas_height:
self.y = -3
if self.hit_paddle(pos) == True:
self.y = -3
if pos[0] <= 0:
self.x = 3
if pos[2] >= self.canvas_width:
self.x = -3
把小球加入主循环:
while 1:
ball.draw()
tk.update_idletasks()
tk.update()
time.sleep(0.01)
23.
list1 = [1, [2], [3, 4], [[5, 6], 7], [8, [[9, [10], 11], 12], 13]]
print([int(i.strip(' []')) for i in str(list1).split(',')])
24.
def leapyear(year):
if (year % 4 == 0 and year % 100) or year % 400 == 0:
return True
else:
return False
def days(ymd):
d =(0,31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30)
day = sum(d[:ymd[1]]) + ymd[2]
if leapyear(ymd[0]) and ymd[1] > 2:
day += 1
return day
start = (2014, 8, 18)
now = (2017, 4, 19)
result = - days(start)
for i in range(start[0], now[0]):
if leapyear(i):
result += 366
else:
result += 365
result = result + days(now)
print(result)
25.
str1 = 'easqWAwaeq'
str2 = ''.join(sorted(list(str1), key = lambda x: ord(x) if ord(x) < 97 else ord(x) -31.5))
print(str2)
26.
lower = int(input("输入区间最小值: "))
upper = int(input("输入区间最大值: "))
for num in range(lower,upper + 1):
# 素数大于 1
if num > 1:
for i in range(2,num):
if (num % i) == 0:
break
else:
print(num)
27.
import itertools
count = 1
for i in itertools.permutations((1, 2, 3, 4, 6 , 7, 8, 9), 4):
list1 = [0] * 9
list1[4] = 5
if len(set([10 - n for n in i]) & set(i)) == 0:
for j in range(4):
list1[j] = i[j]
for j in range(4):
list1[8 - j] = 10 - i[j]
if list1[0] + list1[1] + list1[2] == 15 and list1[0] + list1[3] + list1[6] == 15:
print('No.%d'%count)
for m in range(3):
print(list1[m * 3: (m + 1) * 3])
print()
count += 1
28.
for x in range(1,10):
for y in range(1,10):
for z in range(1,10):
if x + y + z == 15:
print('%s + %s + %s = 15' % (x,y,z))
29.
def fadd(a,b,c,d):
fenzi = a*d+b*c
fenmu = b*d
GCD = lambda x,y:x if y==0 else GCD(y, x%y)
gcd = GCD(fenzi,fenmu)
fenzi /= gcd
fenmu /= gcd
return fenzi, fenmu
print '1/6 + 1/3 = %d/%d' % fadd(1,6,1,3)
def fminus(a,b,c,d):
fenzi = a*d-b*c
fenmu = b*d
GCD = lambda x,y:x if y==0 else GCD(y, x%y)
gcd = GCD(fenzi,fenmu)
fenzi /= gcd
fenmu /= gcd
return fenzi, fenmu
print '1/2 - 1/3 = %d/%d' % fminus(1,2,1,3)
30.
import random as r
guess = ['石头', '剪刀', '布', '石头']
player = input(f'{guess[0:3]}:')
while player != 'Q':
try:
if player not in guess:
raise
CPU = r.choice(guess[0:3])
print(f'CPU:{CPU}')
if player == CPU:
print('Draw!')
elif guess[guess.index(player) + 1] == CPU:
print('Player Win!')
else:
print('CPU win!')
player = input(f'{guess[0:3]}:')
except:
player = input(f'输入有误!{guess[0:3]}:')
(未完待续,持续更新)