Python第一次综合训练报告
Never
Settle
小组信息
组长:徐鹏航
成员名单:徐鹏航,张伟琛,王名扬,郭彤苑
题目:计算24点
# -*- coding:utf-8 -*-
import itertools
a1=[]
for i in range(1,5):
a1.append(str(raw_input('请输入第'+str(i)+'个数字')))
for eachl in itertools.permutations(a1,4):
strlist=[num for num in eachl]
a=str(float(strlist[0]));b=str(float(strlist[1]));c=str(float(strlist[2]));d=str(float(strlist[3]))
try:
for fh1 in ['+','-','*','/']:
for fh2 in ['+','-','*','/']:
for fh3 in ['+','-','*','/']:
if eval('('+a+fh1+b+')'+fh2+c+fh3+d)==24.0:
print '('+a+fh1+b+')'+fh2+c+fh3+d,"=",eval('('+a+fh1+b+')'+fh2+c+fh3+d)
if eval(a+fh1+'('+b+fh2+c+')'+fh3+d)==24.0:
print a+fh1+'('+b+fh2+c+')'+fh3+d,"=",eval(a+fh1+'('+b+fh2+c+')'+fh3+d)
if eval(a+fh1+b+fh2+'('+c+fh3+d+')')==24.0:
print a+fh1+b+fh2+'('+c+fh3+d+')',"= ",eval(a+fh1+b+fh2+'('+c+fh3+d+')')
if eval('('+a+fh1+b+fh2+c+')'+fh3+d)==24.0:
print '('+a+fh1+b+fh2+c+')'+fh3+d,"=",eval('('+a+fh1+b+fh2+c+')'+fh3+d)
if eval(a+fh1+'('+b+fh2+c+fh3+d+')')==24.0:
print a+fh1+'('+b+fh2+c+fh3+d+')',"=",eval(a+fh1+'('+b+fh2+c+fh3+d+')')
if eval('('+a+fh1+b+')'+fh2+'('+c+fh3+d+')')==24.0:
print '('+a+fh1+b+')'+fh2+'('+c+fh3+d+')',"=",eval('('+a+fh1+b+')'+fh2+'('+c+fh3+d+')')
if eval('('+'('+a+fh1+b+')'+fh2+c+')'+fh3+d)==24.0:
print '('+'('+a+fh1+b+')'+fh2+c+')'+fh3+d,"=",eval('('+'('+a+fh1+b+')'+fh2+c+')'+fh3+d)
if eval('('+a+fh1+'('+b+fh2+c+')'+')'+fh3+d)==24.0:
print '('+a+fh1+'('+b+fh2+c+')'+')'+fh3+d,"=",eval('('+a+fh1+'('+b+fh2+c+')'+')'+fh3+d)
if eval(a+fh1+'('+'('+b+fh2+c+')'+fh3+d+')')==24.0:
print a+fh1+'('+'('+b+fh2+c+')'+fh3+d+')',"=",eval(a+fh1+'('+'('+b+fh2+c+')'+fh3+d+')')
if eval(a+fh1+'('+b+fh2+'('+c+fh3+d+')'+')')==24.0:
print a+fh1+'('+b+fh2+'('+c+fh3+d+')'+')',"=",eval(a+fh1+'('+b+fh2+'('+c+fh3+d+')'+')')
except:
ZeroDivisionError
感谢阅读