Python编24点游戏

# !/usr/bin/python
# coding:utf8
'''
介绍: 24点游戏
'''

import itertools, random

numlist = [random.randint(1, 13) for i in range(4)] # 随机4个数
print numlist

nlist = []
[nlist.append(nl) for nl in list(itertools.permutations(numlist)) if nl not in nlist] # 4个数排列组合,并在有重复数字时对组合去重
# print nlist

option = ['+','-','*','/']
olist = list(itertools.product(option,repeat=3)) # 操作符重复组合3位
# print olist

retlist = [str(nl[0])+'*1.0'+ol[0]+str(nl[1])+'*1.0'+ol[1]+str(nl[2])+'*1.0'+ol[2]+str(nl[3])+'*1.0' for nl in nlist for ol in olist] # 拼凑4个数和3个操作符
# print retlist

for rl in retlist:
if eval(rl) == 24: # 计算,每位数*1.0,是防止出现12/5=2的情况
print rl.replace('*1.0','')+'=24' # 去掉'*1.0'

您编写的代码,请问加入括号怎么编程。
如果想结果有无输出两种结果怎么编程

# !/usr/bin/python
# coding:utf8
'''
原始方法编24点游戏
'''

import itertools
import random

numlist = [random.randint(1, 13) for i in range(4)] # 随机4个数
print numlist

nlist = []
[nlist.append(nl) for nl in list(itertools.permutations(numlist)) if nl not in nlist] # 4个数排列组合,并在有重复数字时对组合去重
# print nlist

option = ['+','-','*','/']
olist = list(itertools.product(option,repeat=3)) # 操作符重复组合3位
# print olist

retlist = [[str(nl[0])+'*1.0']+[ol[0]]+[str(nl[1])+'*1.0']+[ol[1]]+[str(nl[2])+'*1.0']+[ol[2]]+[str(nl[3])+'*1.0'] for nl in nlist for ol in olist] # 拼凑4个数和3个操作符
# print retlist

# 括号的位置
# (0,3)(0,5)
# (2,5)(2,7)
# (4,7)
# (0,3)和(4,7)
lastlist = []
for ret in retlist:
    if ('*' not in ret and  '/' not in ret) or ('*' in ret and  '/' in ret):
        lastlist.append(''.join(ret))
    else:
        lastlist.append(''.join(['(']+ret[:3]+[')']+ret[3:]))
        lastlist.append(''.join(['(']+ret[:5]+[')']+ret[5:]))
        lastlist.append(''.join(ret[:2]+['(']+ret[2:5]+[')']+ret[5:]))
        lastlist.append(''.join(ret[:2]+['(']+ret[2:7]+[')']))
        lastlist.append(''.join(ret[:4]+['(']+ret[4:7]+[')']))
        lastlist.append(''.join(['(']+ret[:3]+[')']+ret[3:4]+['(']+ret[4:7]+[')']))
# print lastlist

i = 0
for ll in lastlist:
    try:
        if eval(ll) == 24:  # 计算,每位数*1.0,是防止出现12/5=2的情况
            print ll.replace('*1.0','')+'=24'   # 去掉'*1.0'
        else:
            i += 1
    except ZeroDivisionError, e:
        # print '除于0错误: '+ll.replace('*1.0','')   # 表达式中有除于0的
        i += 1
        continue

if i == len(lastlist):
    print 'no output!'

来自:求助得到的回答
温馨提示:答案为网友推荐,仅供参考
第1个回答  2019-12-23

# !/usr/bin/python

# coding:utf8

'''

原始方法编24点游戏

'''

 

import itertools

import random

 

numlist = [random.randint(1, 13) for i in range(4)]      # 随机4个数

print(numlist)

 

nlist = []

[nlist.append(nl) for nl in list(itertools.permutations(numlist)) if nl not in nlist]  # 4个数排列组合,并在有重复数字时对组合去重

# print nlist

 

option = ['+','-','*','/']

olist = list(itertools.product(option,repeat=3))  # 操作符重复组合3位

# print olist

 

retlist = [[str(nl[0])+'*1.0']+[ol[0]]+[str(nl[1])+'*1.0']+[ol[1]]+[str(nl[2])+'*1.0']+[ol[2]]+[str(nl[3])+'*1.0'] for nl in nlist for ol in olist]   # 拼凑4个数和3个操作符

# print retlist

 

# 括号的位置

# (0,3)(0,5)

# (2,5)(2,7)

# (4,7)

# (0,3)和(4,7)

lastlist = []

for ret in retlist:

    if ('*' not in ret and  '/' not in ret) or ('*' in ret and  '/' in ret):

        lastlist.append(''.join(ret))

    else:

        lastlist.append(''.join(['(']+ret[:3]+[')']+ret[3:]))

        lastlist.append(''.join(['(']+ret[:5]+[')']+ret[5:]))

        lastlist.append(''.join(ret[:2]+['(']+ret[2:5]+[')']+ret[5:]))

        lastlist.append(''.join(ret[:2]+['(']+ret[2:7]+[')']))

        lastlist.append(''.join(ret[:4]+['(']+ret[4:7]+[')']))

        lastlist.append(''.join(['(']+ret[:3]+[')']+ret[3:4]+['(']+ret[4:7]+[')']))

# print lastlist

 

i = 0

for ll in lastlist:

    try:

        if eval(ll) == 24:  # 计算,每位数*1.0,是防止出现12/5=2的情况

            print (ll.replace('*1.0','')+'=24')   # 去掉'*1.0'

        else:

            i += 1

    except ZeroDivisionError:

        # print '除于0错误: '+ll.replace('*1.0','')   # 表达式中有除于0的

        i += 1

        continue

 

if i == len(lastlist):

    print ('no output!')

第2个回答  2015-12-16
“等下有空吗?
相似回答