Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Blackjack error
#3
strd2 starts as 'd2'. You have this bit of code (lines 168-169):

                for strd2 in diepool :
                    damage += d2()
This does not go through diepool and each time there is a 'd2' add d2() to the damage. It goes through each item in die pool, changes strd2 to that item, and then adds d2() to the damage. So for each die in your diepool, you are adding up a d2, a d4, a d6, a d8, a d10, and a d12. That's why your damage is so huge.

You need to think of it this way:

damage = 0
for die in diepool:
    if die == strd2:     # is this really easier than die == 'd2'?
        damage += d2()
    elif die == strd4:
        damage += d4()
    ...
But this leads to a big if/elif chain. There is a simpler way to do this. When you make diepool, instead of appending the string 'd2' to it, you could append the function d2 to it, without calling the function. Then you could do this:

damage = 0
for die in diepool:
    damage += die()
or even simpler: damage = sum(die() for die in diepool).

If that's too confusing, you could just have one die function that takes an integer parameter, and instead of appending 'd2' to diepool, you could append the integer 2 to die pool:

def die(sides):
    return random.randint(sides)

...

damage = 0
for sides in diepool:
    damage += die(sides)
Also note that you can simplify this:

elif dtype != "d2" and dtype != "d4" and dtype != "d6" and dtype != "d8" and dtype != "d10" and dtype != "d12" :
to this:

elif dtype not in ('d2', 'd4', 'd6', 'd8', 'd10', 'd12'):
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Messages In This Thread
Blackjack error - by BlueClaw - Oct-03-2019, 02:42 PM
RE: Blackjack error - by Larz60+ - Oct-03-2019, 03:10 PM
RE: Blackjack error - by ichabod801 - Oct-03-2019, 03:26 PM
RE: Blackjack error - by BlueClaw - Oct-03-2019, 07:37 PM

Forum Jump:

User Panel Messages

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