level 1
将jackie
楼主
import itertools
import tkinter as tk
from tkinter import *
root = tk.Tk()
root.title('24点')
root.geometry("600x400")
tk.Label(root,text='请输入您的数字1:').place(x=120,y=1)
tk.Label(root,text='请输入您的数字2:').place(x=120,y=22)
tk.Label(root,text='请输入您的数字3:').place(x=120,y=42)
tk.Label(root,text='请输入您的数字4:').place(x=120,y=62)
b1 = tk.StringVar()
b2 = tk.StringVar()
b3 = tk.StringVar()
b4 = tk.StringVar()
p1 = tk.Entry(root,textvariable=b1)
p1.pack()
p2 = tk.Entry(root,textvariable=b2)
p2.pack()
p3 = tk.Entry(root,textvariable=b3)
p3.pack()
p4 = tk.Entry(root,textvariable=b4)
p4.pack()
c1 = b1.get()
c2 = b2.get()
c3 = b3.get()
c4 = b4.get()
def do():
try:
pokers = (c1, c2, c3, c4)
ans = set()
count = 0
for nums in itertools.permutations(pokers):
for oper in itertools.product("+-*/", repeat=3):
mode1 = " (({0} {4} {1}) {5} {2} ) {6} {3} ".format(*nums, *oper);
mode2 = " ({0} {4} ({1} {5} {2})) {6} {3} ".format(*nums, *oper);
mode3 = " {0} {4} ({1} {5} ({2} {6} {3}))".format(*nums, *oper);
mode4 = " {0} {4} (({1} {5} {2} ) {6} {3})".format(*nums, *oper);
mode5 = " ({0} {4} {1}) {5} ({2} {6} {3})".format(*nums, *oper);
for express in [mode1, mode2, mode3, mode4, mode5]:
ans.add(express)
for oper in ans:
try:
if abs(eval(oper) - 24) <= 0.0001:
count = count + 1
print(" 第%04d的解法 %s=24" % (count, oper))
except:
continue
except EOFError:
return
button = Button(root,text='计算',height=3,width=15,command=do)
button.pack()
root.mainloop()
2021年08月31日 03点08分
1
import tkinter as tk
from tkinter import *
root = tk.Tk()
root.title('24点')
root.geometry("600x400")
tk.Label(root,text='请输入您的数字1:').place(x=120,y=1)
tk.Label(root,text='请输入您的数字2:').place(x=120,y=22)
tk.Label(root,text='请输入您的数字3:').place(x=120,y=42)
tk.Label(root,text='请输入您的数字4:').place(x=120,y=62)
b1 = tk.StringVar()
b2 = tk.StringVar()
b3 = tk.StringVar()
b4 = tk.StringVar()
p1 = tk.Entry(root,textvariable=b1)
p1.pack()
p2 = tk.Entry(root,textvariable=b2)
p2.pack()
p3 = tk.Entry(root,textvariable=b3)
p3.pack()
p4 = tk.Entry(root,textvariable=b4)
p4.pack()
c1 = b1.get()
c2 = b2.get()
c3 = b3.get()
c4 = b4.get()
def do():
try:
pokers = (c1, c2, c3, c4)
ans = set()
count = 0
for nums in itertools.permutations(pokers):
for oper in itertools.product("+-*/", repeat=3):
mode1 = " (({0} {4} {1}) {5} {2} ) {6} {3} ".format(*nums, *oper);
mode2 = " ({0} {4} ({1} {5} {2})) {6} {3} ".format(*nums, *oper);
mode3 = " {0} {4} ({1} {5} ({2} {6} {3}))".format(*nums, *oper);
mode4 = " {0} {4} (({1} {5} {2} ) {6} {3})".format(*nums, *oper);
mode5 = " ({0} {4} {1}) {5} ({2} {6} {3})".format(*nums, *oper);
for express in [mode1, mode2, mode3, mode4, mode5]:
ans.add(express)
for oper in ans:
try:
if abs(eval(oper) - 24) <= 0.0001:
count = count + 1
print(" 第%04d的解法 %s=24" % (count, oper))
except:
continue
except EOFError:
return
button = Button(root,text='计算',height=3,width=15,command=do)
button.pack()
root.mainloop()