You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何优化dice_roll函数中骰子图案的输出格式(去除多余括号)?

骰子图案无括号打印优化方案

问题说明

我正在为骰子游戏编写一个掷两个六面骰子的dice_roll函数,希望以骰子图案形式展示结果。当前代码可正常运行,但使用numpy数组打印图案时会出现多余括号,想要实现如下干净输出:

[0 0]
[ 0 ]
[0 0]
作为编程新手,除了数组外不清楚其他实现方法,且当前代码需要重复逻辑处理第二个骰子,寻求更优的打印方式。

当前代码(仅处理第一个骰子):

import random
import numpy as np

def dice_roll(): # rolls 2 six-sided dice
  dice1 = random.randint(1,6)
  print("Dice 1: You rolled a", dice1, "\n")

  if dice1 == 1:
    dice_layer1 = np.array(["[     ]"])
    dice_layer2 = np.array(["[  0  ]"])
    dice_layer3 = np.array(["[     ]"])
    roll_animation = np.array([dice_layer1, dice_layer2, dice_layer3])
    print(roll_animation, "\n")

  elif dice1 == 2:
    dice_layer1 = np.array(["[0    ]"])
    dice_layer2 = np.array(["[     ]"])
    dice_layer3 = np.array(["[    0]"])
    roll_animation = np.array([dice_layer1, dice_layer2, dice_layer3])
    print(roll_animation, "\n")
  
  elif dice1 == 3:
    dice_layer1 = np.array(["[0    ]"])
    dice_layer2 = np.array(["[  0  ]"])
    dice_layer3 = np.array(["[    0]"])
    roll_animation = np.array([dice_layer1, dice_layer2, dice_layer3])
    print(roll_animation, "\n")
    
  elif dice1 == 4:
    dice_layer1 = np.array(["[0   0]"])
    dice_layer2 = np.array(["[     ]"])
    dice_layer3 = np.array(["[0   0]"])
    roll_animation = np.array([dice_layer1, dice_layer2, dice_layer3])
    print(roll_animation, "\n")
  
  elif dice1 == 5:
    dice_layer1 = np.array(["[0   0]"])
    dice_layer2 = np.array(["[  0  ]"])
    dice_layer3 = np.array(["[0   0]"])
    roll_animation = np.array([dice_layer1, dice_layer2, dice_layer3])
    print(roll_animation, "\n")
  
  elif dice1 == 6:
    dice_layer1 = np.array(["[0   0]"])
    dice_layer2 = np.array(["[0   0]"])
    dice_layer3 = np.array(["[0   0]"])
    roll_animation = np.array([dice_layer1, dice_layer2, dice_layer3])
    print(roll_animation, "\n")

优化方案

  • 放弃numpy数组,改用普通字符串列表:numpy数组自带格式括号,直接用字符串列表存储每一层图案,逐行打印即可得到无冗余的干净输出。
  • 用字典存储骰子图案,消除重复代码:把每个点数对应的三层图案提前存入字典,掷出点数后直接取值,无需编写大量重复的if-elif分支。
  • 封装打印函数,复用逻辑处理双骰子:编写专门的骰子打印函数,掷出两个骰子后分别调用,也可实现双骰子并排打印。

优化后的完整代码

import random

# 预定义每个点数对应的三层骰子图案
dice_patterns = {
    1: ["[     ]", "[  0  ]", "[     ]"],
    2: ["[0    ]", "[     ]", "[    0]"],
    3: ["[0    ]", "[  0  ]", "[    0]"],
    4: ["[0   0]", "[     ]", "[0   0]"],
    5: ["[0   0]", "[  0  ]", "[0   0]"],
    6: ["[0   0]", "[0   0]", "[0   0]"]
}

def print_dice(number, dice_name):
    print(f"{dice_name}: You rolled a {number}")
    # 逐行打印图案,无多余括号
    for line in dice_patterns[number]:
        print(line)
    print()  # 换行分隔骰子

def dice_roll():
    dice1 = random.randint(1, 6)
    dice2 = random.randint(1, 6)
    
    print_dice(dice1, "Dice 1")
    print_dice(dice2, "Dice 2")

# 调用测试
dice_roll()

进阶:双骰子并排打印

如果希望两个骰子横向并排显示,可修改打印逻辑,将两个骰子的对应行拼接后输出:

def print_two_dice(dice1_num, dice2_num):
    print(f"Dice 1: {dice1_num} | Dice 2: {dice2_num}")
    # 逐行拼接两个骰子的图案
    for line1, line2 in zip(dice_patterns[dice1_num], dice_patterns[dice2_num]):
        print(f"{line1}  {line2}")
    print()

def dice_roll():
    dice1 = random.randint(1, 6)
    dice2 = random.randint(1, 6)
    print_two_dice(dice1, dice2)

dice_roll()

效果说明

优化后的代码输出完全符合干净无括号的要求,同时大幅减少了重复代码,逻辑更清晰,新手也能轻松理解和维护。

内容的提问来源于stack exchange,提问作者chenn_jr

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 09:04:56