用python 学做24点游戏文字版,每次玩10题,每题等10秒出答案
本帖最后由 xyyzzjw 于 2023-7-13 20:53 编辑import random,time
def getResult(a, b, oper):
if oper == '+':
return a + b
if oper == '-':
return a - b
elif oper == '*':
return a * b
elif oper == '/':
return a / b
def express1(question, oper1, oper2, oper3):
"""计算((A_B)_C)_D的式"""
r1 = getResult(question, question, oper1)
r2 = getResult(r1, question, oper2)
r3 = getResult(r2, question, oper3)
return r3
def express2(question, oper1, oper2, oper3):
"""'计算(A_(B_C))_D的形式"""
r1 = getResult(question, question, oper2)
r2 = getResult(question, r1, oper1)
r3 = getResult(r2, question, oper3)
return r3
def express3(question, oper1, oper2, oper3):
"""计算A_(B_C)_D)的形式"""
r1 = getResult(question, question, oper2)
r2 = getResult(r1, question, oper3)
r3 = getResult(question, r2, oper1)
return r3
def express4(question, oper1, oper2, oper3):
"""计算A_(B_(C_D))的形式"""
r1 = getResult(question, question, oper3)
r2 = getResult(question, r1, oper2)
r3 = getResult(question, r2, oper1)
return r3
def express5(question, oper1, oper2, oper3):
"""计算(A_B)_(C_D)的形式:"""
r1 = getResult(question, question, oper1)
r2 = getResult(question, question, oper3)
r3 = getResult(r1, r2, oper2)
return r3
def getsolution(quetion):
"""4个操作数,则有3个操作符,进行全遍历求解"""
global haveQuestion, display
oper = ['+', '-', '*', '/']
for i in range(4):
for j in range(4):
for k in range(4):
if express1(quetion, oper, oper, oper) == 24:
if display:
print("((%d%c%d)%c%d)%c%d" % (
quetion, oper, quetion, oper, quetion, oper, quetion))
haveQuestion = True
if express2(quetion, oper, oper, oper) == 24:
if display:
print("(%d%c(%d%c%d))%c%d" % (
quetion, oper, quetion, oper, quetion, oper, quetion))
haveQuestion = True
if express3(quetion, oper, oper, oper) == 24:
if display:
print("%d%c((%d%c%d)%c%d)" % (
quetion, oper, quetion, oper, quetion, oper, quetion))
haveQuestion = True
if express4(quetion, oper, oper, oper) == 24:
if display:
print("%d%c(%d%c(%d%c%d))" % (
quetion, oper, quetion, oper, quetion, oper, quetion))
haveQuestion = True
if express5(quetion, oper, oper, oper) == 24:
if display:
print("(%d%c%d)%c(%d%c%d)" % (
quetion, oper, quetion, oper, quetion, oper, quetion))
haveQuestion = True
haveQuestion=0
display = 0
num=10
for m in range(num):
while True:
quetion = random.choices(, k=4)
getsolution(quetion)
if haveQuestion:
print("——————————共%d题——————————第%d题:%s"%(num,m+1,quetion))
time.sleep(10)
display = 1
print("第%d题:计算24点的方法如下:"%(m+1))
getsolution(quetion)
print()
time.sleep(1)
haveQuestion=False
display = 0
break 建议使用代码格式 本帖最后由 zhangjinxuan 于 2023-7-13 21:22 编辑
10 秒?https://fishc.com.cn/thread-217193-1-1.html
你这时间复杂度也不至于跑 10 秒吧,我 tkinter 加上最多也只需要 1 秒 Ewan-Ahiouy 发表于 2023-7-13 20:56
建议使用代码格式
论坛上怎么才能用代码格式 zhangjinxuan 发表于 2023-7-13 21:20
10 秒?https://fishc.com.cn/thread-217193-1-1.html
你这时间复杂度也不至于跑 10 秒吧,我 tkinter...
用time.sleep等10秒,这时间让人思考哦。代码运行很快的哦。 xyyzzjw 发表于 2023-7-13 21:31
用time.sleep等10秒,这时间让人思考哦。代码运行很快的哦。
o{:10_256:} xyyzzjw 发表于 2023-7-13 21:30
论坛上怎么才能用代码格式
论坛上怎么才能用代码格式
去掉空格 import random,time
def getResult(a, b, oper):
if oper == '+':
return a + b
if oper == '-':
return a - b
elif oper == '*':
return a * b
elif oper == '/' :
return a / b
def express1(question, oper1, oper2, oper3):
"""计算((A_B)_C)_D的式"""
r1 = getResult(question, question, oper1)
r2 = getResult(r1, question, oper2)
r3 = getResult(r2, question, oper3)
return r3
def express2(question, oper1, oper2, oper3):
"""'计算(A_(B_C))_D的形式"""
r1 = getResult(question, question, oper2)
r2 = getResult(question, r1, oper1)
r3 = getResult(r2, question, oper3)
return r3
def express3(question, oper1, oper2, oper3):
"""计算A_(B_C)_D)的形式"""
r1 = getResult(question, question, oper2)
r2 = getResult(r1, question, oper3)
r3 = getResult(question, r2, oper1)
return r3
def express4(question, oper1, oper2, oper3):
"""计算A_(B_(C_D))的形式"""
r1 = getResult(question, question, oper3)
r2 = getResult(question, r1, oper2)
r3 = getResult(question, r2, oper1)
return r3
def express5(question, oper1, oper2, oper3):
"""计算(A_B)_(C_D)的形式:"""
r1 = getResult(question, question, oper1)
r2 = getResult(question, question, oper3)
r3 = getResult(r1, r2, oper2)
return r3
def getsolution(quetion):
"""4个操作数,则有3个操作符,进行全遍历求解"""
global haveQuestion, display
oper = ['+', '-', '*', '/']
for i in range(4):
for j in range(4):
for k in range(4):
if express1(quetion, oper, oper, oper) == 24:
if display:
print("((%d%c%d)%c%d)%c%d" % (
quetion, oper, quetion, oper, quetion, oper, quetion))
haveQuestion = True
if express2(quetion, oper, oper, oper) == 24:
if display:
print("(%d%c(%d%c%d))%c%d" % (
quetion, oper, quetion, oper, quetion, oper, quetion))
haveQuestion = True
if express3(quetion, oper, oper, oper) == 24:
if display:
print("%d%c((%d%c%d)%c%d)" % (
quetion, oper, quetion, oper, quetion, oper, quetion))
haveQuestion = True
if express4(quetion, oper, oper, oper) == 24:
if display:
print("%d%c(%d%c(%d%c%d))" % (
quetion, oper, quetion, oper, quetion, oper, quetion))
haveQuestion = True
if express5(quetion, oper, oper, oper) == 24:
if display:
print("(%d%c%d)%c(%d%c%d)" % (
quetion, oper, quetion, oper, quetion, oper, quetion))
haveQuestion = True
haveQuestion=0
display = 0
num=10
for m in range(num):
while True:
quetion = random.choices(, k=4)
getsolution(quetion)
if haveQuestion:
print("——————————共%d题——————————第%d题:%s"%(num,m+1,quetion))
time.sleep(10)
display = 1
print("第%d题:计算24点的方法如下:"%(m+1))
getsolution(quetion)
print()
time.sleep(1)
haveQuestion=False
display = 0
break
xyyzzjw 发表于 2023-7-13 21:30
论坛上怎么才能用代码格式
那个<>符号
while True: print("Hello World!")
页:
[1]