Python

Moderators: None (Apply to moderate this forum)
Number of threads: 474
Number of posts: 1166

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Dice script Posted by brento73 on 18 Oct 2002 at 8:19 PM
I'm sure this is simple, but I can't seem to find the solution. What I need is to roll a number of dice(that part I've got down), but to ignore the lowest roll before totalling the rolls.

For instance 3, 5, 6 would be 11, since 3 is ignored

Thanks in advance,

brento73
Report
Re: Dice script Posted by infidel on 21 Oct 2002 at 2:25 PM
: I'm sure this is simple, but I can't seem to find the solution. What I need is to roll a number of dice(that part I've got down), but to ignore the lowest roll before totalling the rolls.

My first thought was to store your individual rolls in a list, then write a function like:

def SumListExcludeLowest(L):
    L.sort()
    total = 0
    for num in L[1:]:
        total += num
    return total

if __name__ == '__main__':
    print SumListExcludeLowest([4,2,5,1])


This just uses the builtin list.sort() method which orders the values, then you iterate through all list elements after the first one. Piece of cake.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.