By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,825 Members | 1,026 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,825 IT Pros & Developers. It's quick & easy.

"rolling a dice" in python. can it be done?

P: 7
simple question. is it possible to write a script that rolls a six sided dice?

if this is possible, is it possible to tell it to roll the six sided dice 4 times and ignore the dice that rolled the lowest score?

if you want to know, i am working on a script to create a dungeons and dragons character :)
May 28 '07 #1
Share this Question
Share on Google+
4 Replies


ilikepython
Expert 100+
P: 844
simple question. is it possible to write a script that rolls a six sided dice?

if this is possible, is it possible to tell it to roll the six sided dice 4 times and ignore the dice that rolled the lowest score?

if you want to know, i am working on a script to create a dungeons and dragons character :)
Ofcourse it is possible, python can do anything! You can use the random module. it has several functions having to do with anything random. Try this link:
http://docs.python.org/lib/module-random.html
May 28 '07 #2

bartonc
Expert 5K+
P: 6,596
simple question. is it possible to write a script that rolls a six sided dice?

if this is possible, is it possible to tell it to roll the six sided dice 4 times and ignore the dice that rolled the lowest score?

if you want to know, i am working on a script to create a dungeons and dragons character :)
Just search TheScripts for "python dice". There are plenty of examples of this solution.
May 28 '07 #3

P: 3
Python. Of course it's possible :)
No claims that this is efficient/pretty/best practise it just does the job.

from random import randrange
diceroll = [0, 0, 0, 0] #initialise a holder for dice rolls
i = 0 #counter for number of rolls
while i < 4: #loop through a dice throw routine
diceroll[i] = randrange(1,7)
i += 1
diceroll.sort() #sort lowest to highest
diceroll = diceroll[1:] #drop the lowest number
print diceroll
Dec 6 '10 #4

Sean Pedersen
P: 30
Joshua and others are right; of course it can be done! I've modified Joshua's code a bit...
Expand|Select|Wrap|Line Numbers
  1. diceroll = [] #initialise a holder for dice rolls
  2. i = 0 #counter for number of rolls
  3. for i in range(4): #loop through a dice throw routine
  4.     diceroll.append(randrange(1,7))
  5.  
  6. diceroll.sort() #sort lowest to highest
  7. diceroll = diceroll[1:] #drop the lowest number
  8. print diceroll 
Dec 6 '10 #5

Post your reply

Sign in to post your reply or Sign up for a free account.