這是一個簡單的擲骰子遊戲應用程序。 用戶可以指定要滾動的骰子數量以及每個骰子的麵數。然後應用程序模擬擲骰子並顯示結果。
這是 Python 中可能的實現:
import random
def roll_dice(num_dice, num_sides):
"""Simulates rolling multiple dice.
Args:
num_dice: The number of dice to roll.
num_sides: The number of sides on each die.
Returns:
A list of integers representing the results of each die roll. Returns an empty list if num_dice is 0 or less.
"""
if num_dice
這段代碼首先定義了一個函數roll_dice
,它將骰子的數量和麵數作為輸入並返回結果列表。 main
函數處理用戶交互,提示輸入並驗證它。 然後它調用 roll_dice
並打印結果和總數。 while True
循環允許用戶多次滾動,直到輸入 0。其中包含錯誤處理以捕獲非整數輸入。 這提供了一個基本但實用的擲骰子應用程序。 可以添加更多高級功能(例如圖形用戶界麵、不同類型的骰子、保存分數)。