python计算24点游戏
时间: 2023-06-05 21:47:29 浏览: 209
24点游戏是一种需要使用数学技巧和逻辑思维的益智游戏,需要将四个数字通过加、减、乘、除的计算方法组合成结果为24的算式。
Python编程语言可以很方便地完成这一任务。首先,我们可以使用一个列表来存储输入的四个数字,然后使用for循环和列表索引来生成所有可能的四个数字的组合。接着,我们可以使用嵌套的for循环和操作符来生成所有可能的计算式子。最后,使用eval函数来计算每个计算式子的值,并判断是否等于24即可。
需要注意的是,在进行除法运算时,需要判断分母是否为0以及分子是否能够整除分母,避免出现错误。
下面是一个简单的Python示例代码:
```
# 输入四个数字
num_list = [1, 2, 3, 4]
# 生成所有可能的数字组合
for i in range(4):
for j in range(4):
if i != j:
for k in range(4):
if k != i and k != j:
for m in range(4):
if m != i and m != j and m != k:
# 生成所有可能的计算式子
s1 = str(num_list[i]) + "+" + str(num_list[j])
s2 = str(num_list[i]) + "-" + str(num_list[j])
s3 = str(num_list[i]) + "*" + str(num_list[j])
if num_list[j] != 0 and num_list[i] % num_list[j] == 0:
s4 = str(num_list[i]) + "/" + str(num_list[j])
else:
s4 = ""
s5 = str(s1) + "+" + str(num_list[k])
s6 = str(s1) + "-" + str(num_list[k])
s7 = str(s1) + "*" + str(num_list[k])
if s4 != "" and num_list[k] != 0 and num_list[i] % num_list[j] == 0:
s8 = str(s1) + "/" + str(num_list[k])
else:
s8 = ""
s9 = str(s2) + "+" + str(num_list[k])
s10 = str(s2) + "-" + str(num_list[k])
s11 = str(s2) + "*" + str(num_list[k])
if s4 != "" and num_list[k] != 0 and num_list[i] % num_list[j] == 0:
s12 = str(s2) + "/" + str(num_list[k])
else:
s12 = ""
s13 = str(s3) + "+" + str(num_list[k])
s14 = str(s3) + "-" + str(num_list[k])
s15 = str(s3) + "*" + str(num_list[k])
if s4 != "" and num_list[k] != 0 and num_list[i] % num_list[j] == 0:
s16 = str(s3) + "/" + str(num_list[k])
else:
s16 = ""
s17 = str(s5) + "+" + str(num_list[m])
s18 = str(s5) + "-" + str(num_list[m])
s19 = str(s5) + "*" + str(num_list[m])
if s8 != "" and num_list[m] != 0 and eval(s4) % num_list[m] == 0:
s20 = str(s5) + "/" + str(num_list[m])
else:
s20 = ""
s21 = str(s6) + "+" + str(num_list[m])
s22 = str(s6) + "-" + str(num_list[m])
s23 = str(s6) + "*" + str(num_list[m])
if s8 != "" and num_list[m] != 0 and eval(s4) % num_list[m] == 0:
s24 = str(s6) + "/" + str(num_list[m])
else:
s24 = ""
s25 = str(s7) + "+" + str(num_list[m])
s26 = str(s7) + "-" + str(num_list[m])
s27 = str(s7) + "*" + str(num_list[m])
if s8 != "" and num_list[m] != 0 and eval(s4) % num_list[m] == 0:
s28 = str(s7) + "/" + str(num_list[m])
else:
s28 = ""
s29 = str(s17) + "+" + str(num_list[i])
s30 = str(s17) + "-" + str(num_list[i])
s31 = str(s17) + "*" + str(num_list[i])
if s20 != "" and num_list[i] != 0 and eval(s8) % num_list[i] == 0:
s32 = str(s17) + "/" + str(num_list[i])
else:
s32 = ""
s33 = str(s18) + "+" + str(num_list[i])
s34 = str(s18) + "-" + str(num_list[i])
s35 = str(s18) + "*" + str(num_list[i])
if s20 != "" and num_list[i] != 0 and eval(s8) % num_list[i] == 0:
s36 = str(s18) + "/" + str(num_list[i])
else:
s36 = ""
s37 = str(s19) + "+" + str(num_list[i])
s38 = str(s19) + "-" + str(num_list[i])
s39 = str(s19) + "*" + str(num_list[i])
if s20 != "" and num_list[i] != 0 and eval(s8) % num_list[i] == 0:
s40 = str(s19) + "/" + str(num_list[i])
else:
s40 = ""
# 计算每个计算式子的值
if eval(s1) == 24 or eval(s2) == 24 or eval(s3) == 24 or eval(s4) == 24 or eval(s5) == 24 or eval(s6) == 24 or eval(s7) == 24 or eval(s8) == 24 or eval(s9) == 24 or eval(s10) == 24 or eval(s11) == 24 or eval(s12) == 24 or eval(s13) == 24 or eval(s14) == 24 or eval(s15) == 24 or eval(s16) == 24 or eval(s17) == 24 or eval(s18) == 24 or eval(s19) == 24 or eval(s20) == 24 or eval(s21) == 24 or eval(s22) == 24 or eval(s23) == 24 or eval(s24) == 24 or eval(s25) == 24 or eval(s26) == 24 or eval(s27) == 24 or eval(s28) == 24 or eval(s29) == 24 or eval(s30) == 24 or eval(s31) == 24 or eval(s32) == 24 or eval(s33) == 24 or eval(s34) == 24 or eval(s35) == 24 or eval(s36) == 24 or eval(s37) == 24 or eval(s38) == 24 or eval(s39) == 24 or eval(s40) == 24:
print("Yes")
```
阅读全文