Python教程

Python-四则运算app初步

本文主要是介绍Python-四则运算app初步,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

话不多说,上代码

  1 from tkinter import *
  2 import random
  3 from fractions import Fraction
  4 
  5 root = Tk()
  6 root.title("四则运算")
  7 root.geometry("605x500+300+100")
  8 
  9 label2 = Label(root,text="请选择下列想要的题型:",font=("微软雅黑",12),fg='green')
 10 label2.place(x=220,y=180)
 11 label3 = Label(root,text="难度等级:初级(0-50)、中级(0-100)、高级(0-200)",font=("微软雅黑",12),fg='red')
 12 label3.place(x=0,y=25)
 13 label4 = Label(root,text="请输入想要的难度:",font=("微软雅黑",12),fg='green')
 14 label4.place(x=120,y=80)
 15 label4 = Label(root,text="请输入想要的题数:",font=("微软雅黑",12),fg='green')
 16 label4.place(x=320,y=80)
 17 
 18 entry1 = Entry()
 19 entry1.place(x=120,y=115)
 20 entry2 = Entry()
 21 entry2.place(x=320,y=115)
 22 level=''
 23 homeworkCount=''
 24 
 25 def easy():
 26     level=entry1.get()
 27     homeworkCount=entry2.get()
 28     print(level)
 29     print(homeworkCount)
 30     button=Button(root,text="确认",command=easy,width=15,height=1)
 31     button.pack()
 32     button.place(x=230,y=145)
 33 
 34 def one():
 35     count=0
 36     level=entry1.get()
 37     homeworkCount=entry2.get()
 38     if level=="初级":
 39         level=50
 40     elif level=="中级":
 41         level=100
 42     else:
 43         level=200
 44     while count<int(homeworkCount):
 45         count=count+1
 46         print("第"+str(count)+"道题目");
 47         intAddSub(level)
 48         print("\n")
 49     print('end!!!')
 50         
 51 def two():
 52     count=0
 53     level=entry1.get()
 54     homeworkCount=entry2.get()
 55     if level=="初级":
 56         level=50
 57     elif level=="中级":
 58         level=100
 59     else:
 60         level=200
 61     while count<int(homeworkCount):
 62         count=count+1
 63         print("第"+str(count)+"道题目")
 64         decAddSub(level)
 65         print("\n")
 66     print('end!!!')
 67         
 68 def three():
 69     count=0
 70     level=entry1.get()
 71     homeworkCount=entry2.get()
 72     if level=="初级":
 73         level=50
 74     elif level=="中级":
 75         level=100
 76     else:
 77         level=200
 78     while count<int(homeworkCount):
 79         count=count+1
 80         print("第"+str(count)+"道题目")
 81     print("\n")
 82     print('end!!!')
 83 
 84 def four():
 85     count=0
 86     level=entry1.get()
 87     homeworkCount=entry2.get()
 88     if level=="初级":
 89         level=50
 90     elif level=="中级":
 91         level=100
 92     else:
 93         level=200
 94     while count<int(homeworkCount):
 95         count=count+1
 96         print("第"+str(count)+"道题目")
 97         intMulDiv(level)
 98     print("\n")
 99     print('end!!!')
100 
101 def five():
102     count=0
103     level=entry1.get()
104     homeworkCount=entry2.get()
105     if level=="初级":
106         level=50
107     elif level=="中级":
108         level=100
109     else:
110         level=200
111     while count<int(homeworkCount):
112         count=count+1
113         print("第"+str(count)+"道题目")
114         fraMulDiv(level)
115         print("\n")
116     print('end!!!')
117 
118 def closewindows():
119     root.destroy()
120 
121 def intAddSub(level):
122     typeCal=random.randint(0,1);     #选择加减运算符
123     num1=random.randint(0,level)    #生成随机整数
124     num2=random.randint(0,level)
125     if typeCal==0:
126         print(str(num1)+"+"+str(num2)+"=?")
127         ans1=input("您的答案是:")
128         print("正确答案是:"+str(num1+num2))
129     else:
130         if num1<num2:
131             t=num1
132             num1=num2
133             num2=t
134         print(str(num1)+"-"+str(num2)+"=?")
135         ans2=input("您的答案是:")
136         print("正确答案是:"+str(num1-num2))
137 
138 def decAddSub(level):
139     typeCal=random.randint(0,1)      #选择加减运算符
140     num1=random.uniform(0,level)   #生成随机整数
141     num1=round(num1,2)
142     num2=random.uniform(0,level)
143     num2=round(num2,2)
144     if typeCal==0:
145         print(str(num1)+"+"+str(num2)+"=?")
146         ans1=input("您的答案是:")
147         print("正确答案是:{:.2f}".format(num1+num2))
148     else:
149         if num1<num2:
150             t=num1
151             num1=num2
152             num2=t
153         print(str(num1)+"-"+str(num2)+"=?")
154         ans2=input("您的答案是:")
155         print("正确答案是:{:.2f}".format(num1-num2))
156 
157 def fraAddSub(level):
158     typeCal=random.randint(0,1)     #选择加减运算符
159     molecule1=random.randint(0,level)    #生成随机整数
160     denominator1=random.randint(1,level)
161     num1=Fraction(molecule1,denominator1)
162     molecule2=random.randint(0,level)
163     denominator2=random.randint(1,level)
164     num2=Fraction(molecule2,denominator2)
165     if typeCal==0:
166         print(str(num1)+"+"+str(num2)+"=?")
167         ans1=input("您的答案是:")
168         print("正确答案是:"+str(num1+num2))
169     else:
170         if num1<num2:
171             t=num1
172             num1=num2
173             num2=t
174         print(str(num1)+"-"+str(num2)+"=?")
175         ans2=input("您的答案是:")
176         print("正确答案是:"+str(num1-num2))
177 
178 def intMulDiv(level):
179     typeCal=random.randint(0,1)     #选择加减运算符
180     num1=random.randint(0,level)   #生成随机整数
181     num2=random.randint(0,level)
182     if typeCal==0:
183         print(str(num1)+"×"+str(num2)+"=?")
184         ans1=input("您的答案是:")
185         print("正确答案是:"+str(num1*num2))
186     else:
187         print(str(num1)+"÷"+str(num2)+"=?")
188         ans2=input("您的答案是:")
189         print("正确答案是:{:.2f}".format(num1/num2))
190 
191 def fraMulDiv(level):
192     typeCal=random.randint(0,1)     #选择加减运算符
193     molecule1=random.randint(0,level)    #生成随机整数
194     denominator1=random.randint(1,level)
195     num1=Fraction(molecule1,denominator1)
196     molecule2=random.randint(0,level);
197     denominator2=random.randint(1,level)
198     num2=Fraction(molecule2,denominator2)
199     if typeCal==0:
200         print(str(num1)+"×"+str(num2)+"=?")
201         ans1=input("您的答案是:");
202         print("正确答案是:"+str(num1*num2))
203     else:
204         print(str(num1)+"÷"+str(num2)+"=?")
205         ans2=input("您的答案是:");
206         print("正确答案是:"+str(num1/num2))
207 
208 btn1 = Button(root,text="整数加减",width=30,height=2,bg="pink",fg="blue",command=one)
209 btn1.pack()
210 btn1.place(x=50,y=220)
211 
212 btn2 = Button(root,text="小数加减",width=30,height=2,bg="pink",fg="blue",command=two)
213 btn2.pack()
214 btn2.place(x=50,y=300)
215 
216 btn3 = Button(root,text="分数加减",width=30,height=2,bg="pink",fg="blue",command=three)
217 btn3.pack()
218 btn3.place(x=50,y=380)
219 
220 btn4 = Button(root,text="整数乘除",width=30,height=2,bg="pink",fg="blue",command=four)
221 btn4.pack()
222 btn4.place(x=320,y=220)
223 
224 btn5 = Button(root,text="分数乘除",width=30,height=2,bg="pink",fg="blue",command=five)
225 btn5.pack()
226 btn5.place(x=320,y=300)
227 
228 btn6 = Button(root,text="退出",width=30,height=2,bg="pink",fg="blue",command=closewindows)
229 btn6.pack()
230 btn6.place(x=320,y=380)
231  
232 root.mainloop()

长这个样子

 混合运算未完待续...

 

这篇关于Python-四则运算app初步的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!