這是一個簡單的擲骰子遊戲應用程序。 用戶可以指定要滾動的骰子數量以及每個骰子的麵數。然後應用程序模擬擲骰子並顯示結果。
這是使用 Python 的可能實現:
import random
def roll_dice(num_dice, num_sides):
"""Rolls a specified number of dice with a specified number of sides.
Args:
num_dice: The number of dice to roll.
num_sides: The number of sides on each die.
Returns:
A list of the results of each die roll.
"""
results = []
for _ in range(num_dice):
results.append(random.randint(1, num_sides))
return results
def main():
"""Gets user input and runs the dice rolling simulation."""
while True:
try:
num_dice = int(input("Enter the number of dice to roll (or 0 to quit): "))
if num_dice == 0:
break
num_sides = int(input("Enter the number of sides on each die: "))
if num_sides
此應用程序提示用戶輸入骰子的數量和麵數。 然後,它使用 random.randint()
函數來模擬擲骰子並顯示各個結果及其總和。 包含錯誤處理來管理無效的用戶輸入。 用戶可以輸入 0 作為骰子數量來退出應用程序。 這是一個基本的例子;可以添加更複雜的功能(例如,圖形用戶界麵、不同類型的骰子、保存結果)。