学号 2021-2022-2 《Python程序设计》实验x报告
课程:《Python程序设计》
班级:2114
姓名: 杨云泰
学号:20211401
实验教师:王志强
实验日期:2022年4月5日
必修/选修: 公选课
1.实验内容
-
设计并完成一个完整的应用程序,完成加减乘除模等运算,功能多多益善。
-
考核基本语法、判定语句、循环语句、逻辑运算等知识点
2. 实验过程及结果
代码
print('步步高家教机,你值得拥有')
run = True
def size():
c=input('输入需要的操作:+,-,*,/')
a=int(input('输入第一个数'))
b=int(input('输入第二个数'))
if c == '+':
result = a + b
elif c == '-':
result = a - b
elif c == '*':
result = a * b
elif c == '/':
if b == 0:
print('除数不能为零!!!!')
result = "错误!!!!"
else:
result = a / b
print(f'{a}{c}{b}={result}\n')
def fushu():
c=input('输入需要的操作:+,-,*,/')
a=complex(input('输入第一个复数'))
b=complex(input('输入第二个复数'))
if c == '+':
result = a + b
elif c == '-':
result = a - b
elif c == '*':
result = a * b
elif c == '/':
result = a / b
print(f'{a}{c}{b}={result}\n')
while run:
print('四则运算请输入:1\n复数运算请输入:2\n退出请输入:0')
mod = input()
if mod == '1':
size()
elif mod == '2':
fushu()
elif mod == '0':
run = False
break
- 在老师的基础上,用函数封装,让代码可观性更强。
运行截图
用tkinter库做的计算器
import tkinter as tk
from tkinter import messagebox
def ask_close():
ask = messagebox.askokcancel('警告 :', '你要退出计算器吗?')
if ask == True:
window.destroy()
exit(0)
def display(x):
t.insert('insert', str(x))
equal()
def clear():
t.delete('1.0', 'end')
t_result.delete('1.0', 'end')
def dele():
len1 = len(t.get('1.0', 'insert'))
len2 = len1 - 1
t.delete('1.' + str(len2), '1.' + str(len1))
equal()
def equal():
str1 = t.get('1.0', 'end')
string = str1.replace('%', '/100').replace('x', '*')
t_result.delete('1.0', 'end')
try:
num = round(eval(string), 8)
t_result.insert('end', num)
except:
num = ''
t_result.insert('end', num)
def equalall():
str1 = t.get('1.0', 'end')
string = str1.replace('%', '/100').replace('x', '*')
t_result.delete('1.0', 'end')
try:
num = round(eval(string), 8)
t.delete('1.0', 'end')
t.insert('end', num)
except:
num = 'Wrong Expression!'
t_result.insert('end', num)
if __name__ == '__main__':
try:
window = tk.Tk()
window.title('计算器')
window.geometry('500x540')
window.protocol('WM_DELETE_WINDOW', ask_close)
window.resizable(width=False, height=False)
tk.Label(window, bg='lavenderblush', width=100, height=100).place(x=0, y=0)
#第一排的按钮
b_C = tk.Button(window, text='C', width=10, height=3, command=clear, font=('Arial',12), bg='plum' )
b_C.place(x=50,y=140)
b_division = tk.Button(window, text='/', width=10, height=3, command=lambda : display('/'), font=('Arial', 12), bg='plum')
b_division.place(x=152, y=140)
b_multipy = tk.Button(window, text='x', width=10, height=3, command=lambda : display('x'), font=('Arial', 12), bg='plum')
b_multipy.place(x=254, y=140)
b_delete = tk.Button(window, text='<=', width=10, height=3, command=dele, font=('Arial', 12), bg='plum')
b_delete.place(x=356, y=140)
# 第二排的按钮
b_7 = tk.Button(window, text='7', width=10, height=3, command=lambda : display('7'), font=('Arial', 12), bg='lavender')
b_7.place(x=50, y=210)
b_8 = tk.Button(window, text='8', width=10, height=3, command=lambda : display('8'), font=('Arial', 12), bg='lavender')
b_8.place(x=152, y=210)
b_9 = tk.Button(window, text='9', width=10, height=3, command=lambda : display('9'), font=('Arial', 12), bg='lavender')
b_9.place(x=254, y=210)
b_subtraction = tk.Button(window, text='--', width=10, height=3, command=lambda : display('-'), font=('Arial', 12), bg='plum')
b_subtraction.place(x=356, y=210)
# 第三排的按钮
b_4 = tk.Button(window, text='4', width=10, height=3, command=lambda : display('4'), font=('Arial', 12), bg='lavender')
b_4.place(x=50, y=280)
b_5 = tk.Button(window, text='5', width=10, height=3, command=lambda : display('5'), font=('Arial', 12), bg='lavender')
b_5.place(x=152, y=280)
b_6 = tk.Button(window, text='6', width=10, height=3, command=lambda : display('6'), font=('Arial', 12), bg='lavender')
b_6.place(x=254, y=280)
b_addition = tk.Button(window, text='+', width=10, height=3, command=lambda : display('+'), font=('Arial', 12), bg='plum')
b_addition.place(x=356, y=280)
# 第四排的按钮
b_1 = tk.Button(window, text='1', width=10, height=3, command=lambda : display('1'), font=('Arial', 12), bg='lavender')
b_1.place(x=50, y=350)
b_2 = tk.Button(window, text='2', width=10, height=3, command=lambda : display('2'), font=('Arial', 12), bg='lavender')
b_2.place(x=152, y=350)
b_3 = tk.Button(window, text='3', width=10, height=3, command=lambda : display('3'), font=('Arial', 12), bg='lavender')
b_3.place(x=254, y=350)
b_equalall = tk.Button(window, text='=', width=10, height=7, command=equalall, font=('Arial', 12), bg='blueviolet')
b_equalall.place(x=356, y=348)
# 第五排的按钮
b_percent = tk.Button(window, text='%', width=10, height=3, command=lambda : display('%'), font=('Arial', 12), bg='lavender')
b_percent.place(x=50, y=420)
b_0 = tk.Button(window, text='0', width=10, height=3, command=lambda : display('0'), font=('Arial', 12), bg='lavender')
b_0.place(x=152, y=420)
b_spot = tk.Button(window, text='.', width=10, height=3, command=lambda : display('.'), font=('Arial', 12), bg='lavender')
b_spot.place(x=254, y=420)
# 显示栏
t = tk.Text(window, height=1, width = 24, font=('Arial',24))
t.place(x=35,y=30)
t_result = tk.Text(window, height=1, width=24, font=('Arial', 24))
t_result.place(x=35, y=72)
window.mainloop()
except SystemExit:
pass
运行截图
3. 实验过程中遇到的问题和解决过程
- 问题1:复数的乘法与除法怎么编写
- 问题1解决方案:在网上查,发现Python自带复数运算
其他(感悟、思考等)
用类与对象封装是极其必要的,在做较复杂的程序时,按照功能封装在不同类中不仅使代码可读性增强,还让在修改代码时准确找到位置,这是我所欠缺的。