猫史档案馆


月光之树

月光之树

Lv.1

看到上面那个蓝色的东西了吗↑等他变成黄色,我就来读评论

获赞:2212收藏:629浏览:36909作品收藏:1241

签名:2首页 全是新喵 点赞分200/1000 收藏分500/1000 在创作分1000/1000达成

回复帖子评论
上一页1 页 / 共 5下一页

星空工作市市长申请改名换姓(室长必看) 中回复

同意,换啥

2023-01-18T18:26:19 点赞:0

星空工作市市长申请改名换姓(室长必看) 中回复

改好了

2023-01-18T18:38:21 点赞:0

【官方】如何上首页?只需在本帖下面评论! 中回复

编程猫社区-NY os 1.0开源-少儿编程作品 (codemao.cn)emotion_编程猫_点赞

2023-01-21T17:57:00 点赞:0

【官方】如何上首页?只需在本帖下面评论! 中回复

一定要通过呀!emotion_编程猫_加油

2023-01-31T14:32:06 点赞:0

工作室3.5版,正式出炉! 中回复

星云公社招人了!

无门槛!无门槛!无门槛!

通过快,通过快,通过快!

星云公社是你最好的选择!

2023-02-07T13:47:15 点赞:0

明天期末考 中回复

我今天考的,7月2日出成绩

2023-06-27T22:51:13 点赞:0

Python作品:24点 中回复

代码还有没完成的,

check_solution

这个函数下,int后列表的项数要自己改

 

2023-06-27T22:55:12 点赞:0

Python作品:24点 中回复

开发完成

最终代码

import tkinter as tk
from tkinter import messagebox
import itertools
import operator
import random
def calculate(a, b, op):
    if op == "+":
        return a + b
    if op == "-":
        return a - b
    if op == "*":
        return a * b
    if op == "/":
        return a / b if b != 0 else None
def solve_24(nums, target=24):
    ops = ["+", "-", "*", "/"]
    num_permutations = list(itertools.permutations(nums))
    op_permutations = list(itertools.product(ops, repeat=3))
    results = []
    for num_perm in num_permutations:
        for op_perm in op_permutations:
            expr = f"({num_perm[0]} {op_perm[0]} {num_perm[1]}) {op_perm[1]} {num_perm[2]} {op_perm[2]} {num_perm[3]}"
            try:
                result = eval(expr)
            except ZeroDivisionError:
                result = None
            if result == target:
                results.append(expr)
    return results
def check_solution():
    user_solution = user_input.get()
    solution_nums = user_solution
    solution_nums = solution_nums.replace("(", " ")
    solution_nums = solution_nums.replace(")", " ")
    solution_nums = solution_nums.replace("+", " ")
    solution_nums = solution_nums.replace("-", " ")
    solution_nums = solution_nums.replace("*", " ")
    solution_nums = solution_nums.replace("/", " ")
    user_solution_nums = [s for s in solution_nums.split() if s.isdigit()]
    print(user_solution_nums)
    num_bool = 1
    if len(user_solution_nums) == 4:
        x = int(user_solution_nums[0])
        if x in nums:
            if num_bool:
                num_bool = 1
            else:
                num_bool = 0
        else:
            num_bool = 0
            user_input.delete(0, tk.END)
        x = int(user_solution_nums[1])
        if x in nums:
            if num_bool:
                num_bool = 1
            else:
                num_bool = 0
        else:
            num_bool = 0
            user_input.delete(0, tk.END)
        x = int(user_solution_nums[2])
        if x in nums:
            if num_bool:
                num_bool = 1
            else:
                num_bool = 0
        else:
            num_bool = 0
            user_input.delete(0, tk.END)
        x = int(user_solution_nums[3])
        if x in nums:
            if num_bool:
                num_bool = 1
            else:
                num_bool = 0
        else:
            num_bool = 0
            user_input.delete(0, tk.END)
    else:
        num_bool = 0
    if num_bool == 0:
        messagebox.showerror("结果", "不包含生成的数字")
        user_input.delete(0, tk.END)
    try:
        result = eval(user_input.get())
    except (SyntaxError, NameError, ZeroDivisionError):
        result = None
    if result == 24 and num_bool:
        messagebox.showinfo("结果", "正确!")
        generate_valid_numbers()  # 生成新的数字
    else:
        if num_bool == 1:
            messagebox.showerror("结果", "错误!")
            user_input.delete(0, tk.END)
def generate_valid_numbers():
    global nums, solutions, timer
    while True:
        nums = [random.randint(1, 13) for _ in range(4)]
        solutions = set(solve_24(nums))
        if solutions:
            break
    for i, num in enumerate(nums):
        num_vars[i].set(num)
    user_input.delete(0, tk.END)
    timer = "未开启计时模式"
    timer_var.set(timer)
def show_solution():
    if solutions:
        solution_str = list(solutions)[random.randint(0, len(solutions) - 1)]
        messagebox.showinfo("答案", f"一个可能的答案:{solution_str}")
        generate_valid_numbers()
    else:
        messagebox.showinfo("答案", "无解!")
def update_timer():
    global timer, timer_expired
    if isinstance(timer, int):
        if timer > 0:
            timer -= 1
            timer_var.set(f"剩余时间:{timer}秒")
            root.after(1000, update_timer)
        elif not timer_expired:
            timer_var.set("时间到!")
            timer_expired = True
            show_solution()
def start_timer():
    global timer, timer_expired
    timer = 50
    timer_expired = False
    update_timer()
def hint():
    if solutions:
        user_input.delete(0, tk.END)
        user_input.insert(0, list(solutions)[random.randint(0, len(solutions) - 1)])
root = tk.Tk()
root.title("24点")
num_vars = [tk.StringVar() for _ in range(4)]
num_labels = [tk.Label(root, textvariable=num_var, font=("Arial", 24)) for num_var in num_vars]
for i, num_label in enumerate(num_labels):
    num_label.grid(row=0, column=i)
user_input = tk.Entry(root, width=30)
user_input.grid(row=1, column=0, columnspan=4)
check_button = tk.Button(root, text="检查解答", command=check_solution)
check_button.grid(row=2, column=0)
generate_button = tk.Button(root, text="生成数字", command=generate_valid_numbers)
generate_button.grid(row=2, column=1)
start_button = tk.Button(root, text="开始计时", command=start_timer)
start_button.grid(row=2, column=2)
hint_button = tk.Button(root, text="提示", command=hint)
hint_button.grid(row=2, column=3)
timer_var = tk.StringVar()
timer_label = tk.Label(root, textvariable=timer_var)
timer_label.grid(row=3, column=0, columnspan=4)
generate_valid_numbers()
root.mainloop()

2023-06-28T16:28:55 点赞:0

【Python作品分享】chatGPTpython2.0【作品秀】 中回复

你这api key怎么有一个喵

2023-06-30T17:28:48 点赞:0

【Python作品分享】chatGPTpython2.0【作品秀】 中回复

这api该不会中间的哪个喵是 sb

2023-06-30T17:31:55 点赞:1

az,大家来看看,用chatgpt写的α-β博弈树剪枝算法的五子棋ai 中回复

又一次,gpt4写的哈

正括号“[”

反括号“)”

2023-06-30T17:34:45 点赞:0

玩了一会铁盒五子棋,控制台多了一点东西 中回复

真亿点东西

2023-06-30T20:23:43 点赞:0

【测试】加个符号秃然变‘色’ 中回复

吃♂喵

2023-07-01T17:21:54 点赞:0

http;//shepu.codman.com 中回复

 

 

 

 

2023-07-01T17:29:01 点赞:0

http;//shepu.codman.com 中回复

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

2023-07-01T17:29:17 点赞:0

又发现了一个编程猫网站的bug 中回复

<script type="text/javascript">alert("你好");</script>

2023-07-03T09:03:22 点赞:0

官网的BUG 中回复

#include<bits/stdc++.h>

#include<bits/stdc++.h>

2023-07-20T15:14:13 点赞:0

想找工作室 中回复

怎么又是你

2023-07-23T16:06:39 点赞:0

找工作室)()(( 中回复

https://shequ.codemao.cn/work_shop/17841

2023-07-23T16:08:20 点赞:0

想找工作室 中回复

难道就我一个不招的?????

2023-07-23T16:36:16 点赞:0

【铁盒五子棋】8.8W注册用户 中回复

sofa

2023-07-24T22:04:48 点赞:0

我有一个好同桌 中回复

《刚刚》

2023-07-24T22:05:34 点赞:1

什么是肝帝?他/她说 中回复

1,

有人相信我今年1月创建的项目

到现在还没有做完吗?

2023-07-25T20:12:47 点赞:0

【KittenV4.1.1版本更新】超有料!克隆可以按编号识别? 中回复

emotion_编程猫_紧张

2023-07-27T11:31:00 点赞:0

【KittenV4.1.1版本更新】超有料!克隆可以按编号识别? 中回复

emotion_编程猫_紧张

2023-08-03T12:04:12 点赞:0

【官方】如何上首页?只需在本帖下面评论! 中回复

2023-08-03T16:49:43 点赞:0

工作室招人! 中回复

打错了,是“择”

2023-08-05T10:59:37 点赞:0

dalao小号找工作室 中回复

来我们这,只要申请2小时必过

2023-08-07T16:22:31 点赞:0

【官方】如何上首页?只需在本帖下面评论! 中回复

希望再上一次首页

2023-08-08T10:10:03 点赞:0

猜系列(24期)(编程猫用户) 中回复

一个emoji

2023-08-08T21:03:16 点赞:0