小弟写了一个算三八二十四的程序
import string
import copy
def calc():
a = raw_input('Enter the first number: ')
b = raw_input('Enter the second number: ')
c = raw_input('Enter the third number: ')
d = raw_input('Enter the fourth number: ')
aList = [a, b, c, d]
bList = ['+', '-', '*', '/']
i = 0
while (i < 4):
w = aList[i]
j = 0
while (j < 4):
if j == i :
j = j+1
continue
x = aList[j]
k = 0
while (k<4):
if k == i or k == j :
k = k+1
continue
y = aList[k]
l = 0
while (l<4):
if l== i or l == j or l == k :
l = l+1
continue
z = aList[l]
m = 0
while (m<4):
op1 = bList[m]
n = 0
while (n<4):
op2 = bList[n]
o = 0
while (o<4):
op3 = bList[o]
List1 = ['float('+w+')', op1, 'float('+x+')', op2, 'float('+y+')', op3, 'float('+z+')']
verify(List1)
List2 = copy.copy(List1)
addBrackets(List1, 0, 3)
verify(List1)
List3 = copy.copy(List2)
addBrackets(List2, 2, 5)
verify(List2)
List4 = copy.copy(List3)
addBrackets(List3, 4, 7)
verify(List3)
List5 = copy.copy(List4)
addBrackets(List4, 0, 5)
verify(List4)
List6 = copy.copy(List5)
addBrackets(List5, 2, 7)
verify(List5)
List7 = copy.copy(List6)
addBrackets(List6, 4, 7)
addBrackets(List6, 0, 3)
verify(List6)
o = o+1
n = n+1
m = m+1
l = l+1
k = k+1
j = j+1
i = i+1
def verify(List):
str = ''
for eachItem in List:
str = str + eachItem
try :
result = eval(str)
except ZeroDivisionError :
result = 0;
if result-24<0.001 and result-24>-0.001 :
print str + '=24'
def addBrackets(List, begin, end):
List.insert(end, ')')
List.insert(begin, '(')
calc()