一、前言
本文是博主初学python所完成的几项课程实训,主要包括面向过程、基于GUI界面、面向对象三种形式的猜数字游戏、基于协同过滤算法的电影打分与推荐、演员关系分析、实现验证码图片生成器、实现猜拳游戏、编写主程序整合所有功能,初学python还有许多不足,大家在阅读过程中发现有可以改进的地方欢迎私信或者评论区与我讨论。
二、实训内容
(一)面向过程的猜数字游戏
1)程序要求
设计一个python程序,可以实现一个简单的猜数字游戏。用户在终端通过输入数字来猜测答案。程序会根据用户的猜测给出相应的提示,直到用户猜对为止。
2)实现步骤
1.随机生成一个1~100之间的整数作为答案;
2.提示用户输入猜测的数字,并将其转换为整数类型;
3.判断用户猜测的数字是否等于答案,如果是则给出“恭喜你,猜对了!”的提示并告诉用户一共猜了几次,结束游戏,否则继续执行下一步;
4.判断用户猜测的数字是否大于答案,如果是则给出“猜大了,请重新输入!”的提示,否则给出“猜小了,请重新输入!”的提示;
5.重复执行步骤2~4,直到用户猜对为止。
3)解题关键
1.使用random模块生成随机数;
2.使用while循环来实现重复执行;
3.使用input函数来获取用户输入;
4.使用if语句来实现条件判断。
构思出关键步骤就能很快写出目标程序了~
4)源代码
import random
c = random.randint(1,100)
s = 0
while True:
a = int(input("请输入1至100以内的任意一个整数:"))
s +=1
if c > a:
print("猜小了,请重新输入!","您猜的次数是:",s)
elif c < a:
print("猜大了,请重新输入!","您猜的次数是:",s)
else:
print("恭喜你,猜对了!","您猜的次数是:",s)
break
5)程序运行情况
(二)基于GUI界面的猜数字游戏
1)程序要求
设计一个python程序,可以实现一个简单的猜数字游戏。该游戏可以在GUI界面中运行,用户可以通过输入数字来猜测答案。程序会根据用户的猜测给出相应的提示,直到用户猜对为止。
2)实现步骤
1.在GUI界面中添加一个文本框用于显示游戏提示和结果。
2.随机生成一个1~100之间的整数作为答案。
3.在GUI界面中添加一个输入框用于用户输入猜测的数字,并添加一个按钮用于提交猜测。
4.当用户点击提交按钮时,将输入框中的内容转换为整数类型,并判断用户猜测的数字是否等于答案,如果是则在文本框中显示“恭喜你,猜对了!”的提示并告诉用户一共猜了几次,结束游戏,否则继续执行下一步。
5.判断用户猜测的数字是否大于答案,如果是则在文本框中显示“猜大了,请重新输入!”的提示,否则在文本框中显示“猜小了,请重新输入!”的提示。
6.重复执行步骤4~5,直到用户猜对为止。
3)解题关键
1.使用tkinter模块创建GUI界面;
2.使用Entry组件创建输入框,使用Button组件创建按钮;
3.使用Label组件创建文本框,并使用config方法修改文本框中的内容。
4)源代码
from tkinter import *
import random
c = random.randint(1, 100)
s = 0
def csz():
a = e.get()
x = int(a)
global s
s += 1
if c > x:
print("猜小了,请重新输入!")
label = Label(f, text="猜小了,请重新输入", bg='lightblue')
label.pack()
label.place(x=100, y=150)
elif c < x:
print("猜大了,请重新输入!")
label = Label(f, text="猜大了,请重新输入", bg='lightblue')
label.pack()
label.place(x=100, y=150)
else:
print("恭喜你,猜对了!", s)
label = Label(f, text="恭喜你,猜对了!您猜的次数是:{}".format(s), bg='lightblue')
label.pack()
label.place(x=100, y=150)
f = Tk()
f.title('猜数字')
f.geometry('300x220')
f.config(bg='lightblue')
label_1 = Label(text='请输入1-100以内的任意整数', font=("Arial", 12), bg='lightblue')
label_1.pack()
label_1.place(x=60, y=10)
h = Label(text='输入:', font=("Arial", 10))
h.pack()
h.place(x=40, y=80)
e = Entry(f)
e.pack()
e.place(x=80, y=80)
n = Button(text='确定', command=csz)
n.pack()
n.place(x=100, y=180, width=80)
f.mainloop()
5)程序运行情况
(三)面向对象实现猜数字游戏
1)程序要求
针对实践“基于GUI界面的猜数字游戏”程序功能要求,使用面向对象程序设计方法改写该程序。
2)实现步骤
与“基于GUI界面的猜数字游戏”相同,只是需要采用面向对象方法书写程序。
3)解题关键
1.把所有可视对象作为类的成员,并在_init_()中定义;
2.根据功能需求创建成员方法实现游戏效果。
4)源代码
import random
import tkinter as tk
class number:
def checkanswer(self):
self.count += 1
guess = int(self.entry.get())
if guess == self.answer:
self.result.config(text="恭喜你,猜对了!,你一共猜了{}次".format(self.count))
self.entry.config(state=tk.DISABLED)
elif guess > self.answer:
self.result.config(text="猜大了,请重新输入!")
else:
self.result.config(text="猜小了,请重新输入!")
def __init__(self, window):
self.window = window
self.answer = random.randint(1, 100)
self.count = 1
self.window.title("猜数字游戏")
self.label = tk.Label(self.window, text="请输入1~100之间的整数:", font=("Arial", 12))
self.label.pack()
self.entry = tk.Entry(self.window, font=("Arial", 12))
self.entry.pack()
self.button = tk.Button(self.window, text="提交", command=self.checkanswer, font=("Arial", 12))
self.button.pack()
self.result = tk.Label(self.window, text="", font=("Arial", 12))
self.result.pack()
window = tk.Tk()
game = number(window)
window.mainloop()
5)程序运行情况
(四)基于协同过滤算法的电影打分与推荐
1)程序要求
设计一个python程序,采用协同过滤算法实现简单的电影打分与推荐
2)实现步骤
编写程序,生成数据模拟(也可以使用真实数据,这里博主采用的是模拟数据)多人对多个电影的打分(1-5 分),然后根据这些数据对某用户 A 进行推荐。
推荐规则为:在已有数据中选择与该用户 A 的爱好最相似的用户 B,然后从最相似的用户 B 已看过但用户 A 还没看过的电影中选择用户 B 打分最高的电影推荐给用户 A。
相似度的计算标准为:
1.两个用户共同打分过的电影越多,越相似;
2.两个用户对共同打分的电影的打分越接近,越相似。
3)解题关键
1.使用random模块生成一些随机的数据作为已知的用户评分数据;
2.数据结构是一个字典列表,每个元素代表一个用户的评分信息,键是电影名称,值是评分;
3.以字典形式定义一个当前用户及其评分信息,用于寻找最相似的其他用户;
4.定义一个匿名函数,根据两个用户共同评价过的电影数量以及他们评分的平方差的总和来计算两个用户之间的相似度;
5.从最相似用户的评分中找出当前用户没有评价过的电影,并推荐评分最高的那部电影给当前用户。
4)源代码
from random import randrange
print('已知数据:')
for i in range(10):
data={'movie'+str(randrange(1,15)):randrange(1,6)for a in range(randrange(3,10))}
data1={'user'+str(i):data}
print(data1)
user={'movie'+str(randrange(1,15)):randrange(1,6)for i in range(5)}
e=lambda item:(-len(item[1].keys()&user),sum(((item[1].get(movie)-user.get(movie))**2 for movie in user.keys()&item[1].keys())))
similaruser,movies=min(data1.items(),key=e)
for item in data1.items():
print(len(item[1].keys()&user.keys()),sum(((item[1].get(movie)-user.get(movie))**2 for movie in user.keys()&item[1].keys())),item,sep=':')
print('当前用户:')
print(user)
print('相似度最高的用户的电影评分:')
print(similaruser,movies,sep=':')
print('推荐电影:')
print(max(movies.keys()-user.keys(), key=lambda movie:movies[movie]))
5)程序运行情况
(五)演员关系分析
1)程序要求
现有xlsx文件,内容如下图:
要求统计所有演员中关系最好的n个演员及其共同参演电影数量(n>=2),这里关系好的定义为共同参演的电影数量最多。
2)实现步骤
编写程序,使用python扩展库读取Excel文件中的数据,返回一个字典,字典的键为演员姓名,值为该演员参演的电影名称的集合,读取数据时,跳过表头,对于每一行的有效数据,获取每一行的电影名称和演员清单,对该电影的参演演员进行分割得到演员列表,列表中的每个演员都参演过该行对应的电影。
3)解题关键
1.使用有关openpyxl库的方法实现
4)源代码
只用很简短的代码就可以实现了~
import openpyxl
book = openpyxl.load_workbook('实践素材.xlsx')
sheet = book.active
cells = sheet['A2':'B19']
for c1,c2 in cells:
a=("{0:8}{1:8}".format(c1.value, c2.value))
dict1 = {c1.value: c2.value}
print(dict1)
5)程序运行情况
(六)演员关系分析程序的改写以及功能补充
1)程序要求
在完成的演员关系分析程序代码上进一步实现,打印输出关系最好的n个演员共同参演的电影名称。
2)实现步骤
与上一部分相似。
3)解题关键
1.加载Excel文件遍历每一行数据,构建演员-电影映射关系;
2.使用reduce合并所有演员参与的电影集合;
3.生成所有可能的演员组合,计算每个组合共演电影的数量。
4)源代码
from functools import reduce
from itertools import combinations
import openpyxl
def getactors(filename):
actors = dict() # 定义一个字典用来存放结果
wb = openpyxl.load_workbook(filename)
ws = wb.worksheets[0]
# 遍历文件信息
for index, row in enumerate(ws.rows):
if index == 0:
continue
filmname, actor = row[0].value, row[1].value.split('/')
for a in actor:
actors[a] = actors.get(a, set())
actors[a].add(filmname)
return actors
data = getactors('实践素材.xlsx')
print(data)
def relations(num):
allfilms = reduce(lambda x, y: x | y, data.values(), set())
combidata = combinations(data.items(), num)
truelove = max(combidata, key=lambda item: len(reduce(lambda x, y: x & y, [i[1] for i in item], allfilms)))
return('关系最好的{0}个演员是{1}'
'ta们共同主演的电影数量是{2},'
'ta们共同主演的电影是'.format(num, tuple((item[0] for item in truelove)),
len(reduce(lambda x, y: x & y,
[item[1] for item in truelove],
allfilms))), reduce(lambda x, y: x & y, [item[1] for item in truelove], allfilms))
print(relations(2))
print(relations(3))
print(relations(4))
5)程序运行情况
(七)实现验证码图片生成器
1)程序要求
设计一个python程序,使其运行时能随机生成验证码图片并显示,要求图片显示的内容为“字母+数字”。
要求:
1.安装python的pillow扩展库;
2.熟悉pillow库Image,ImageDraw,ImageFont模块的用法;
3.了解验证码图片的作用;
4.熟悉验证码图片的生成原理
2)实现步骤
编写程序,首先导入用于生成随机数、定义验证码字符集和图像处理等的外部库, 使用相关板块来定义包含字母和数字的字符集, 定义函数通过指定方法从字符集中随机选择字符,组成指定长度的字符串,然后进行版面美观程度调整,可通过偏移像素来扭曲图像,使图像看起来更自然,并随机在图像上添加点和线来增加干扰元素,最后将生成的图像保存到指定路径并显示出来。
3)解题关键
1.善用random库,懂得各种随机数、随机颜色、随机线条生成方法
2.定义字符集,并使用特定方法从字符集中随机选择字符;
3.通过像素偏移实现扭曲效果,增强验证码的真实感,并添加随机的点和线条作为干扰元素,使验证码更加难以被自动识别;
4.使用save和show方法保存和展示最终的图像。
4)源代码
from random import choice, randint, randrange
import string
from PIL import Image, ImageDraw, ImageFont
characters = string.ascii_letters+string.digits
def selectedCharacters(length):
result = ''.join(choice(characters) for _ in range(length))
return result
def getColor():
r = randint(0, 255)
g = randint(0, 255)
b = randint(0, 255)
return ( r, g, b )
def main(size=(200,100),
characterNumber = 6,
bgcolor = (255, 255,255)):
imageTemp = Image.new('RGB', size, bgcolor)
draw = ImageDraw.Draw(imageTemp)
text = selectedCharacters(characterNumber)
font = ImageFont.truetype('c:\\windows\\fonts\\TIMESBD.TTF', 48)
width, height = draw.textsize(text, font)
if width+2*characterNumber>size[0] or height>size[1]:
print('尺寸不合法')
return
startX = 0
widthEachCharacter = width//characterNumber
for i in range(characterNumber):
startX += widthEachCharacter + 1
position = (startX,(size[1]-height)//2+randint(-10, 10))
draw.text(xy=position, text=text[i],
font=font, fill=getColor())
imageFinal = Image.new('RGB', size, bgcolor)
pixelsFinal = imageFinal.load()
pixelstemp = imageTemp.load()
for y in range(size[1]):
offset = randint(-1, 0)
for x in range(size[0]):
newx = x+offset
if newx >= size[0]:
newx = size[0]-1
elif newx < 0:
newx = 0
pixelsFinal[newx, y] = pixelstemp[x, y]
draw = ImageDraw.Draw(imageFinal)
for i in range(int(size[0]*size[1]*0.07)):
draw.point((randrange(size[0]), randrange(size[1])),
fill=getColor())
for i in range(8):
start = (0, randrange(size[1]))
end = (size[0], randrange(size[1]))
draw.line([start, end], fill=getColor(), width=1)
for i in range(8):
start = (-50, -50)
end = (size[0]+10, randint(0, size[1]+10))
draw.arc(start+end, 0, 360, fill=getColor())
imageFinal.save("result.jpg")
imageFinal.show()
if __name__ == "__main__":
main((200, 100), 6, (255, 255, 255))
5)程序运行情况
(八)实现猜拳游戏
1)程序要求
参照猜数字游戏实现剪刀石头布猜拳游戏。
用户通过点击剪刀、石头或布对应的图片猜拳,电脑随机出拳,根据猜拳规则显示输赢情况。
要求:
1.用户通过点击图片按钮出拳,计算机通过label标签出拳,然后在标签中显示结果;
2.计算机也通过显示剪刀石头布图片出拳然后显示结果。
2)解题关键
原理和猜数字游戏一样,只需要解决石头剪刀布的按钮布局、按钮传参并写出判断输赢的标准就可以了~
1.使用tkinter在button上显示图片。
3)源代码
源代码里是采用相对路径引用图片,大家如果也采用这个形式的话,要记得把图片和程序放在同一个文件夹。
import tkinter as tk
import random
class game():
def __init__(self, scream):
self.scr = scream
self.scr.title("猜拳游戏")
# 创建剪刀石头布的照片
self.scissors_img = tk.PhotoImage(file="剪刀.png")
self.scissors_img = self.scissors_img.subsample(3, 3)
self.rock_img = tk.PhotoImage(file="石头.png")
self.rock_img = self.rock_img.subsample(3, 3)
self.paper_img = tk.PhotoImage(file="布.png")
self.paper_img = self.paper_img.subsample(3, 3)
# 创建剪刀石头布的按钮
self.jd_button = tk.Button(self.scr, image=self.scissors_img, command=lambda: self.check("剪刀"))
self.st_button = tk.Button(self.scr, image=self.rock_img, command=lambda: self.check("石头"))
self.bu_button = tk.Button(self.scr, image=self.paper_img, command=lambda: self.check("布"))
# 创建计算机出拳标签
self.computer_img = tk.Label(self.scr)
# 创建结果标签
self.result_label = tk.Label(self.scr)
# 将按钮和标签添加到主窗口中
self.jd_button.pack(side=tk.LEFT, padx=10)
self.st_button.pack(side=tk.LEFT, padx=10)
self.bu_button.pack(side=tk.LEFT, padx=10)
self.computer_img.pack(pady=10)
self.result_label.pack(pady=10)
def check(self, choice):
computer_choice = random.choice(["剪刀", "石头", "布"])
# 随机生成计算机出拳
if computer_choice == "剪刀":
self.computer_img.config(image=self.scissors_img)
elif computer_choice == "石头":
self.computer_img.config(image=self.rock_img)
else:
self.computer_img.config(image=self.paper_img)
# 判断胜负
if choice == computer_choice:
result = "哈哈,平局!"
elif (choice == "剪刀" and computer_choice == "布")or \
(choice == "石头" and computer_choice == "剪刀")or \
(choice == "布" and computer_choice == "石头"):
result = "哼!竟然被你赢了"
else:
result = "你输啦!笨蛋~"
# 显示结果
self.result_label.config(text=f"玩家出拳:{choice}\n计算机出拳:{computer_choice}\n{result}")
if __name__ == "__main__":
scream = tk.Tk()
game1 = game(scream)
scream.mainloop()
4)程序运行情况
(九)程序整合
1)整合要求
编写一个主程序,制作一个菜单,如:
1.面向过程的猜数字游戏
2.基于GUI界面的猜数字游戏
3.面向对象的猜数字游戏
4.基于协同过滤算法的电影打分与推荐
......
输入序号执行相应程序
2)源代码
import subprocess
import tkinter as tk
class main2():
def __init__(self, window1):
self.window1 = window1
self.window1.title("python课程实践")
self.label = tk.Label(self.window1,
text="请选择需要执行的程序:\n"
"1-----面向过程的猜数字游戏\n"
"2-----基于GUI界面的猜数字游戏\n"
"3-----基于协同过滤算法的电影打分与推荐\n"
"4-----演员关系分析\n"
"5-----演员关系分析程序的改写以及功能补充\n"
"6-----面向对象实现猜数字游戏\n"
"7-----验证码图片生成器\n"
"8-----猜拳游戏", font=("Arial", 12))
self.label.pack()
self.entry = tk.Entry(self.window1, font=("Arial", 12))
self.entry.pack()
self.button = tk.Button(self.window1, text="提交", command=self.work, font=("Arial", 12))
self.button.pack()
def work(self):
# def work_1(self):
n = self.entry.get()
if n == '1':
subprocess.call(['python', 'work1.py'])
elif n == '2':
subprocess.call(['python', 'work2.py'])
elif n == '3':
subprocess.call(['python', 'work3.py'])
elif n == '4':
subprocess.call(['python', 'work4.py'])
elif n == '5':
subprocess.call(['python', 'work5.py'])
elif n == '6':
subprocess.call(['python', 'work6.py'])
elif n == '7':
subprocess.call(['python', 'work7.py'])
elif n == '8':
subprocess.call(['python', 'work8.py'])
if __name__ == '__main__':
window1 = tk.Tk()
main2(window1)
window1.mainloop()
3)程序运行情况
4)优化版(其实也不是很优化,显示还是不够美观)
用tkinter实现一个GUI界面,点击按钮展示运行结果截图,并在下方显示编程体会:
5)优化版源代码
from tkinter import *
from PIL import Image, ImageTk
root = Tk()
root.title('python课程实践')
photo1 = None
img1 = None
photo2 = None
img2 = None
photo3 = None
img3 = None
photo4 = None
img4 = None
photo5 = None
img5 = None
photo6 = None
img6 = None
photo7 = None
img7 = None
photo8 = None
img8 = None
def show():
global photo1
global img1
img1 = Image.open('1.png') # 打开图片
photo1 = ImageTk.PhotoImage(img1) # 用PIL模块的PhotoImage打开
imglabel = Label(root, image=photo1)
imglabel.place(x=100)
k = Label(root, text="当时做的时候纠结了累加的语句应该放在哪里,现在看感觉不难了。", font=("Arial", 12))
k.place(x=100, y=550)
def show1():
global photo2
global img2
img2 = Image.open('2.png') # 打开图片
photo2 = ImageTk.PhotoImage(img2) # 用PIL模块的PhotoImage打开
imglabel = Label(root, image=photo2)
imglabel.place(x=100)
k = Label(root, text="一开始不知道怎么解决局部变量不能改变全局变量的问题,后来查了在循环里面加global就可以解决了。",font=("Arial", 12))
l = Label(root, text="GUI界面内容排版的时候,感觉用pack()定位不太精准,尝试了place()排版定位,感觉好了很多,",font=("Arial", 12))
h = Label(root, text="也理解了三种定位方式的使用方法。", font=("Arial", 12))
k.place(x=100, y=540)
l.place(x=100, y=560)
h.place(x=100, y=580)
def show2():
global photo3
global img3
img3 = Image.open('3.png')
# 打开图片
photo3 = ImageTk.PhotoImage(img3) # 用PIL模块的PhotoImage打开
imglabel = Label(root, image=photo3)
imglabel.place(x=100)
k = Label(root, text="做的过程中逐渐理解了协同过滤算法的运行原理。", font=("Arial", 12))
k.place(x=100, y=550)
def show3():
global photo4
global img4
img4 = Image.open('4.png') # 打开图片
photo4 = ImageTk.PhotoImage(img4) # 用PIL模块的PhotoImage打开
imglabel = Label(root, image=photo4)
imglabel.place(x=100)
k = Label(root, text="学会了python中openpyxl库的使用方法,将读取到的值进行分割存入字典。", font=("Arial", 12))
k.place(x=100, y=550)
def show4():
global photo5
global img5
img5 = Image.open('5.png') # 打开图片
photo5 = ImageTk.PhotoImage(img5) # 用PIL模块的PhotoImage打开
imglabel = Label(root, image=photo5)
imglabel.place(x=100)
k = Label(root, text="改写程序过程中我不太理解lambda表达式的应用,分析了程序运行过",
font=("Arial", 12))
l = Label(root, text="程之后,在return语句中多添加了一条lambda表达式,实现了输出关",
font=("Arial", 12))
h = Label(root, text="系最好的n个演员共同参演的电影名称,对lambda表达式有了更进一", font=("Arial", 12))
o = Label(root, text="步的理解。", font=("Arial", 12))
k.place(x=100, y=480)
l.place(x=100, y=500)
h.place(x=100, y=520)
o.place(x=100, y=540)
def show5():
global photo6
global img6
img6 = Image.open('6.png') # 打开图片
photo6 = ImageTk.PhotoImage(img6) # 用PIL模块的PhotoImage打开
imglabel = Label(root, image=photo6)
imglabel.place(x=100)
k = Label(root, text="我将可视对象在__init__定义与过程代码分离开,理解了面向对象程序", font=("Arial", 12))
l = Label(root, text="设计的基本结构,记住了一些典型方法的作用和写法", font=("Arial", 12))
k.place(x=100, y=450)
l.place(x=100, y=470)
def show6():
global photo7
global img7
img7 = Image.open('7.png') # 打开图片
photo7 = ImageTk.PhotoImage(img7) # 用PIL模块的PhotoImage打开
imglabel = Label(root, image=photo7)
imglabel.place(x=100)
k = Label(root, text="在读写程序的过程中,我了解了pillow中Image,ImageDraw,ImageFont", font=("Arial", 12))
l = Label(root, text="模块的用法,对验证码图片的作用和生成原理有了认识", font=("Arial", 12))
k.place(x=100, y=550)
l.place(x=100, y=570)
def show7():
global photo8
global img8
img8 = Image.open('8.png') # 打开图片
photo8 = ImageTk.PhotoImage(img8) # 用PIL模块的PhotoImage打开
imglabel = Label(root, image=photo8)
imglabel.place(x=100)
k = Label(root, text="做的时候想将石头剪刀布转化成数字,然后设置if循环语句来判断输", font=("Arial", 12))
l = Label(root, text="赢,通过按钮传递参数的时候遇到了问题,后来用lambda解决了传递", font=("Arial", 12))
h = Label(root, text="参数的问题,学会了怎么用tkinter在button上显示图片,让计算机通", font=("Arial", 12))
o = Label(root, text="过label标签出拳,并在标签中显示结果。", font=("Arial", 12))
k.place(x=100, y=450)
l.place(x=100, y=470)
h.place(x=100, y=490)
o.place(x=100, y=510)
a = Button(root, text="实践内容一", command=show)
a.place(x=0, y=0)
b = Button(root, text="实践内容二", command=show1)
b.place(x=0, y=50)
c = Button(root, text="实践内容三", command=show2)
c.place(x=0, y=100)
d = Button(root, text="实践内容四", command=show3)
d.place(x=0, y=150)
e = Button(root, text="实践内容五", command=show4)
e.place(x=0, y=200)
f = Button(root, text="实践内容六", command=show5)
f.place(x=0, y=250)
g = Button(root, text="实践内容七", command=show6)
g.place(x=0, y=300)
h = Button(root, text="实践内容八", command=show7)
h.place(x=0, y=350)
mainloop()
6)优化版程序运行情况
(十)实训小结
1)实训过程中遇到的问题及解决方法
1.GUI页面排版问题
解决方法:查找了pack()、grid()、place()布局管理器的概念和使用方式,选择grid布局管理器网格、表格的方式将每个组件的行、列、列宽、行宽、对齐方式和方向间距精确设定,实现了排版的有序。
2.验证码面向对象程序设计改写
解决方法:设定了类,一句句理解验证码程序,将可视对象分离出来在__init__里面定义。
3.猜拳游戏中,计算机也通过显示剪刀石头布图片出拳
解决方法:在__init__里面设定好石头剪刀布的图片,另外定义一个函数,由if判断语句组成,通过条件判断自动调用显示计算机出拳的图片。
2)程序中还存在哪些问题及下一步改进计划
1.图片自适应窗口大小问题
改进计划:对图片对象进行缩放,让它在一个矩形框内保持原来的比例,在后面设计一个期望图像显示的大小,以PIL图像对象打开要插入的图像并获取图像的原始大小,用Label将图片大小显示到指定的显示框。
2.循环语句的嵌套运用
改进计划:理解好每个循环语句的运用,多尝试运用不同的循环语句书写程序,在书写程序的过程中根据程序要求可以多尝试循环语句嵌套。
以上就是博主初学python所做实训的全部内容,希望能对广大求学者有所帮助!
未经允许,请勿转载