用户:
非主流名字查看:0 回复:0 评论:0 创建时间:2023-08-06T20:56:09
【作品展示】

【作品介绍】
厌倦了枯燥而浪费时间的流程?这个plus版二十四点绝对能惊艳到你!
耗时两年半(bushi)制作而成,
自动检测可解性,
不仅避免枯燥流程‘
还从根源上解决刷卡组!
真!T!M!一!举!两!得!
【作品源代码】
import random
def generate_numbers():
while True:
numbers = random.sample(range(1, 14), 4) # 从1到13中随机选择4个数作为牌组
if is_solvable(numbers):
return numbers
def is_solvable(numbers):
operators = ['+', '-', '*', '/']
# 对所有可能的数字排列和运算符组合进行计算
for a in numbers:
for b in numbers:
if b == a:
continue
for c in numbers:
if c == a or c == b:
continue
for d in numbers:
if d == a or d == b or d == c:
continue
for op1 in operators:
for op2 in operators:
for op3 in operators:
expression = f"(({a} {op1} {b}) {op2} {c}) {op3} {d}"
try:
result = eval(expression)
if result == 24:
return True
except ZeroDivisionError:
continue
return False
def evaluate_expression(expression):
try:
result = eval(expression)
return result
except ZeroDivisionError:
return None
def play_game():
numbers = generate_numbers()
print("欢迎来到二十四点游戏!")
print("你的牌组是:", numbers)
while True:
expression = input("请给出一个表达式,使用牌组中的数字计算(输入 q 退出游戏): ")
if expression == 'q':
print("游戏结束。再见!")
break
result = evaluate_expression(expression)
if result is None:
print("表达式无效,请重新输入。")
else:
if result == 24:
print("恭喜!你成功得到了24点!")
break
else:
print("计算结果为:", result)
play_game()
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cn