-
Notifications
You must be signed in to change notification settings - Fork 131
/
Copy pathrolldice.py
32 lines (23 loc) · 941 Bytes
/
rolldice.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import random
def roll_dice(num_dice=1, num_sides=6):
if num_dice < 1 or num_sides < 2:
return "Invalid input. Please provide valid values for the number of dice and sides."
rolls = [random.randint(1, num_sides) for _ in range(num_dice)]
return rolls
def main():
print("Welcome to the Dice Rolling Game!")
while True:
num_dice = int(input("Enter the number of dice to roll: "))
num_sides = int(input("Enter the number of sides on each die: "))
rolls = roll_dice(num_dice, num_sides)
print("\nRoll result:")
if num_dice == 1:
print(f"You rolled a {rolls[0]}!")
else:
print(f"You rolled: {', '.join(map(str, rolls))} (Total: {sum(rolls)})")
play_again = input("\nRoll the dice again? (yes/no): ")
if play_again.lower() != "yes":
break
print("Thanks for playing!")
if __name__ == "__main__":
main()