Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    19
    Rep Power
    0

    Dice-rolling App


    Greeetings,

    A buddy of mine has asked if I can help him write a dice-rolling app that will do the following tasks:

    1. Roll 2-9 six-sided dice (D6).
    2. Take the two highest values among the dice rolled.
    3. Add those two values together.

    As a side task, since his main goal is to compile statistics, he'd like the app to be able to do this over and over and keep 1) the number of times it's run, and 2) what the current average score is.

    ----------------

    So, that's what I'm tasked with doing for him (I volunteered), and I need some help as I'm a newb to the extreme. So far I have only the part that takes in the argument for number of dice:

    Code:
    import system
    
    # Define the number of dice being rolled
    nod = sys.argv[1]
    if len(sys.argv) != 2:
      print "Please enter a single number between 2 and 9."
    I Googled and found some reference to using <b>random</b> to do my dice rolling via random.rand(1, 6) , but that turned out to be deprecated.

    Anyway, if you guys could offer some hints on how to do the randomness in Python 2.3 I'd appreciate it. I don't want someone to write this for me; I just need some guidance. I can't find any good documentation on it for the later versions of Python; all I keep seeing is the random.rand stuff.

    Thanks much.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    11
    well the module random(as shown) is the thing for doing random stuff.

    As some helpers of mine say don't reinvent the wheel:P

    There are several attributes you can use in the random module. Like random.choice(list)
  4. #3
  5. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    This should illustrate some of the functions you could (I would) use to create your program, as well as some of the steps needed. I think this should already do what you wanted:

    Code:
    >>> randomNumbers1 = [random.randint(1, 6) for r in xrange(2, random.randint(2, 9))]
    >>> randomNumbers1
    [1, 4, 5, 1]
    >>>
    >>> max(randomNumbers1)
    5
    >>>
    >>> randomNumbers2 = [random.randint(1, 6) for r in xrange(2, random.randint(2, 9))]
    >>> randomNumbers2
    [6, 6]
    >>> 
    >>> max(randomNumbers2)
    6
    >>>  
    >>> print 'Total is', max(randomNumbers1) + max(randomNumbers2)
    Total is 11
    >>>
    Hope this helps,

    Mark.
    programming language development: www.netytan.com Hula

  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    19
    Rep Power
    0
    Actually, I think my description was slightly misunderstood (possibly my fault). I wanted to be able to supply the number of 6-sided dice between 2 and 9, roll them, and then take the two highest values from them. Those two values would then be added together.

    Thanks to your code, however, I've learned a lot and have come up with mostly working code to get me the list:

    Code:
    import sys
    import random
    
    # Define the number of dice (nod) and make sure it's from 2-9
    nod = sys.argv[1]
    if len(sys.argv) != 2:
      print "Please enter a single number between 2 and 9."
    
    # Roll the dice "nod" number of times
    dr = [random.randint(1, 6) for i in xrange(int(nod))]
    print dr
    At this point I have only two problems: 1) I want to be able to handle when the user forgets to enter an argument to the app; as of now it gives a Python error whereas I'd rather write a nice user-friendly usage guide when that happens. Secondly, I need to be able to take the two highest values out of my list -- not just the single highest.

    Once I get this solved I can easily add them; from there I can probably come up with a way to do this over and over and save the numbers into an average.

    Any ideas on how to fix these two issues?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25
    well for the testing the input, just place something like this after the input statment
    Code:
    if not the_input:
        print "sorry you must inter some input"
        sys.exit(1)
    no for the 2 highest scores you could just remove the last highest and then look for the new highest like so:
    Code:
    highNum = max(list)
    list.remove(highNum)
    secHighNum = max(list)
    and for your last question you can just create a loop that does the above multibles times and save it to a list then at the end print it out.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    394
    Rep Power
    51
    Hi!

    To get the 2 hightest values, you can do this:
    Code:
    dr = [random.randint(1,6) for i in xrange(9)]
    dr.sort()
    print dr[-2:]
    Regards, mawe
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25
    Originally Posted by mawe
    Hi!

    To get the 2 hightest values, you can do this:
    Code:
    dr = [random.randint(1,6) for i in xrange(9)]
    dr.sort()
    print dr[-2:]
    Regards, mawe
    Yes, I have no idea why i didn't put it that way, this is a much better way for python, well it really doesn't do anything diff just more pythonic than mine, I would have givin you some rebutaion points but it says i have already givin you some.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    19
    Rep Power
    0
    Here's what I am up till now; heh, I ended up using pop to get my top two numbers. That just goes to show you how many ways there are to skin a cat.

    Code:
    !/usr/bin/python
    
    # roll -- A dice-rolling application that rolls 2-9 d6
    #  and adds the highest two values.
    
    import sys
    import random
    
    # Make sure an argument is given when the program is run,
    #  and make sure it's only one (nop).
    try:
        nop = len(sys.argv)
    except:
        sys.exit("roll -- A dice rolling application that rolls 2-9 d6 and adds the highest two values.\nUsage: roll <number of dice>.")
    
    if nop != 2:
        sys.exit("roll -- A dice rolling application that rolls 2-9 d6 and adds the highest two values.\n\nUsage: roll <number of dice>.")
    
    # Take in the number of d6 to be rolled, ensuring something is entered
    try:
        nod = sys.argv[1]
        print "You have rolled " + nod + " dice..."
    except:
        sys.exit("Usage: roll <number of dice>. \nPlease retry.")
    
    # Make sure nod is a number between 2 and 9.
    # r = range(2,10)
    # if (nod not in r):
    #     sys.exit("Please enter a number of dice between 2 and 9.")
    
    # Roll the dice "nod" number of times
    dr = [random.randint(1, 6) for i in xrange(int(nod))]
    
    # Print the unsorted list of rolls and then sort the list
    print "Your rolls were: " + str(dr)
    dr.sort()
    
    # Pop off the top two values from the list and store them seperately
    
    highest = dr.pop()
    print "Your highest roll was: " + str(highest) + "."
    next_highest = dr.pop()
    print "Your next highest roll was: " + str(next_highest) + "."
    
    total = highest + next_highest
    print "Your total was: " + str(total) + "."
    I totally appreciate all your help, and I still may need more help doing the second part. I'm having trouble getting decimal numbers out of the average of all the list contents. I want to roll like 1000 d6 and average them. It should come out to roughly 3.5, but it's being rounded off to 3.

    Anyone know how to fix that?
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25
    it proabbly depends, you need to post your code up, but looking at your current code, something i noticed that isn't really bad persay but more or less redundante is the use of str(blah) for printing veribles. Python takes care of that for you, so you don't have to turn it into a string to print it.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    394
    Rep Power
    51
    Originally Posted by danielrm26
    I want to roll like 1000 d6 and average them. It should come out to roughly 3.5, but it's being rounded off to 3.
    Code:
    dr = [random.randint(1,6) for i in xrange(1000)]
    av = sum(dr)/1000.0    # the sum divided by a float -> float
    print av
    Regards, mawe
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    19
    Rep Power
    0
    Originally Posted by mawe
    Code:
    av = sum(dr)/1000.0    # the sum divided by a float -> float
    print av
    Ah, but since I need to divide by
    Code:
    len(dr)
    , I need to convert that value to a float value.

    Any ideas on doing that? I tried float(len(dr)), but it just gave me a floated version of the whole number.
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    19
    Rep Power
    0
    Originally Posted by CyBerHigh
    it proabbly depends, you need to post your code up, but looking at your current code, something i noticed that isn't really bad persay but more or less redundante is the use of str(blah) for printing veribles. Python takes care of that for you, so you don't have to turn it into a string to print it.
    I was getting errors on not being able to combine strings with lists.
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25
    Originally Posted by danielrm26
    I was getting errors on not being able to combine strings with lists.
    ee sorry, i must have missed that it was a list somewhere. Sorry, i just scimd the code and just noticed that and i asumed wrong. but ya thats right you can't.
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    394
    Rep Power
    51
    Hi!

    Originally Posted by danielrm26
    I tried float(len(dr)), but it just gave me a floated version of the whole number.
    Hm, I guess I don't really understand what you want, because I have no problem doing this:
    Code:
    dr = [random.randint(1,6) for i in xrange(1000)]
    av = sum(dr)/float(len(dr))
    print av
    which gives ~ 3.5..
    You can also write
    Code:
    av = float(sum(dr))/len(dr)
    Regards, mawe
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    78
    Rep Power
    10
    to get float division when using "/":
    Code:
    from __future__ import division
    
    print 3/2
    print 4/2
    print 3//2
    print 4//2
    "//" is integer division. It's nicer to do the single import than have to constantly cast to float

    --OH.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo