24点意见反馈与问答

代码:
函数:

import itertools
import random

def tw(cards):
    for nums in itertools.permutations(cards):
        for ops in itertools.product('+-*/', repeat=3):
            bds1 = '({0}{4}{1}){5}({2}{6}{3})'.format(*nums,*ops)
            bds2 = '(({0}{4}{1}){5}{2}){6}{3}'.format(*nums,*ops)
            bds3 = '{0}{4}({1}{5}({2}{6}{3}))'.format(*nums,*ops)
            for bds in [bds1,bds2,bds3]:
                try:
                    if abs(eval(bds) - 24.0) < 1e-10:
                        return bds
                except ZeroDivisionError:
                    continue
    return False

def translate():
    ncards = [0,0,0,0]
    cards = rcard()
    report = check(cards)
    for i in range(len(cards)):
        if cards[i] == 1:
            ncards[i] = 'A'
        elif cards[i] == 11:
            ncards[i] = 'J'
        elif cards[i] == 12:
            ncards[i] = 'Q'
        elif cards[i] == 13:
            ncards[i] = 'K'
        else:
            ncards[i] = str(cards[i])
    return ncards,report

def check(cards):
    report = tw(cards)
    return report

def rcard():
    lst = [random.randint(1,13),
           random.randint(1,13),
           random.randint(1,13),
           random.randint(1,13)]
    return lst

主程序:

import itertools
import random
import function
from function import *
import datetime

level = 1

print('欢迎来到24点游戏中心!')

while True:
    a = input('\n您可以输入q键退出\n')
    if a == 'q':
        break

    flag = True

    if level <= 20:
        minute = 5
    elif level <= 40:
        minute = 4
    elif level <= 60:
        minute = 3
    elif level <= 100:
        minute = 2
    else:
        minute = 1

    print('第{}关'.format(level))
    print('时间:{}min'.format(minute))
    
    c,r = translate()
    for i in range(3):
        print(c[i],end = ' ')
    print(c[3],end = '\n')
    
    time1 = datetime.datetime.now()
    ans = input('A:我解出来了\nB:我认为无解\n')
    time2 = datetime.datetime.now()
    timedel = time2 - time1
    if timedel > datetime.timedelta(minutes = minute):
        print('时间太久了!')
        flag = False
        continue
    
    if ans == 'A':
        bds = input('请输入您计算出来的算式,×用*,÷用/。\n')
        numlst = []
        f = False
        
        for i in range(len(bds)):
            st = ''
            if f:
                f = False
                continue
            f = False
            
            if bds[i] >= '0' and bds[i] <= '9':
                st += bds[i]
                if i == len(bds) - 1:
                    break
                if bds[i + 1] >= '0' and bds[i + 1] <= '9':
                    st += bds[i + 1]
                    f = True
                numlst.append(st)
                
        flag2 = True
        numlst2 = list(numlst)
        
        for i in range(len(numlst)):
            if numlst[i] == '1':
                numlst[i] = 'A'
            elif numlst[i] == '11':
                numlst[i] = 'J'
            elif numlst[i] == '12':
                numlst[i] = 'Q'
            elif numlst[i] == '13':
                numlst[i] = 'K'
            if numlst[i] in c:
                numlst2.pop(0)
                
        if numlst2:
            flag2 = False
            
        if int(eval(bds)) == 24 and flag2 == True:
            print('恭喜您答对了!')
        else:
            print('您答错了。')
            continue
        
    elif ans == 'B':
        if r == False:
            print('这道题确实无解。')
        else:
            print('这道题有解。')
            print('如:')
            print(r)
            continue

    else:
        print('输入错误!')
        continue
        
    level += 1
不知道你这个问题是否已经解决, 如果还没有解决的话:

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 以帮助更多的人 ^-^