1 回答

TA貢獻(xiàn)1860條經(jīng)驗(yàn) 獲得超8個(gè)贊
與其打印擲骰子,不如將其添加到列表中。
import random
faces = int(input("How many sides on a die? ")
num_dice = int(input("How many dice? ")
rolls = []
for die in range(num_dice):
dice_roll = random.randint(1, faces)
rolls.append(dice_roll)
print("You have rolled: " + str(rolls))
然后你可以對(duì)可讀性進(jìn)行一些小的改進(jìn),比如重構(gòu)這個(gè)
rolls = []
for die in range(num_dice):
dice_roll = random.randint(1, faces)
rolls.append(dice_roll)
進(jìn)入這個(gè)
rolls = [random.randint(1, faces) for _ in range(num_dice)]
甚至使用字符串格式或 f 字符串進(jìn)行打印。
print("You have rolled: {}".format(rolls))
# or
print(f"You have rolled: {rolls}")
添加回答
舉報(bào)