Python Forum
My first try with python
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
My first try with python
#1
So, I wanted to play D&D with my friends and I had an idea. What if I code a program to roll the dice for me?
Here is my first try, I don't know anything about coding, so it kind of sucks.
Any advice is welcome.
Thank you all :3

import random
def Random():
	while True:
		try:
			dice = int(input("How many dice? "))
			break
		except:
			print("That's not a valid option!")
	while True:
		try:
			side = int(input("How many sides? "))
			break
		except:
			print("That's not a valid option!")
	x = 0
	total = 0
	while x < dice:
		x = x+1
		rolls = random.randint (1, side)
		total = total+rolls
		print rolls
	print "Total =", total
	Random()
Random()
Reply
#2
import random

rolls = int(input('Number of rolls: '))

for roll in rolls:
    dice = random.choice(list(range(1,7)))
    print(dice)
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
The number of sides doesn't clarify the start / end points, and d100s have gaps. I would probably hard-code the different dice as options, and maybe have an input like "2d20" so that the count and type can be specified at the same time.

When you catch the exception from user input, you want to catch the specific kind of exception, not a more generic one, so as not to accidentally suppress something you weren't considering.
Reply
#4
Quote:maybe have an input like "2d20"
I thought about that, but I don't really know how to do that (°Д°)
Reply
#5
Oh hey, D&D!  I also once wrote a roller: http://dnd.nilamo.com/
It's different from what you're working on, though, as mine was made for character generation (roll 4d6, 6 times for a row, for 6 rows to make a grid, reroll all 1s, and add the three highest numbers to get a stat, then choose any row/column for your base stats).
 
(Aug-07-2017, 08:32 PM)MaxAim Wrote: I thought about that, but I don't really know how to do that
Something like:
>>> response = input("Roll: ")
Roll: 2d20
>>> number_of_dice, type_of_dice = map(int, response.lower().split("d"))
>>> number_of_dice
2
>>> type_of_dice
20
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020