#πŸ”’ rpg stat roller

14 messages Β· Page 1 of 1 (latest)

thorn kraken
#

https://paste.pythondiscord.com/Y4SQ

I have been working on learning python partially by building this stat generator as a pet project.

This is a newly rewritten iteration incorporating much of what I have learned so far.

I am seeking help with the "get_rolls" function - seems like i could make that a less verbose for loop, but I can't get it working.

I am also seeking experienced people to say things like "why didn't you just do xyz?"

I have taken in a lot in the last couple weeks. If you have already graced me with advice, and it doesn't look like I'm running with the ball, it might bear repetition, which I would be grateful for.

narrow quailBOT
#

@thorn kraken

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

dusky perch
#

get_rolls can be return [roll(1,6) for _ in range(4)]

thorn kraken
#

i need a list

dusky perch
#

this is a list comprehension

thorn kraken
#

oh

dusky perch
#

Also, I consider it a bad idea to have heterogenous lists like https://paste.pythondiscord.com/Y4SQ#1L25-L25. You have a list in which the first 3 elements are rolls, and then there's a sum of these 3 and another, discarded roll. it'll be very annoying to remember/look up which elements are which

thorn kraken
#

would you just use separate variables?

#

list comprehension works nicely .. i know about it, and it's the next chapter in the guide i'm working through :P

dusky perch
#

!e so I'd do something like this:

import random
from dataclasses import dataclass


@dataclass
class RollResult:
    raw_rolls: list[int]

    @property
    def cull(self):
        return min(self.raw_rolls)

    @property
    def sum(self):
        return sum(self.raw_rolls) - self.cull

    @property
    def unculled(self):
        lst = self.raw_rolls.copy()
        lst.remove(self.cull)
        return lst


def get_rolls() -> RollResult:
    return RollResult(raw_rolls=[random.randint(1, 6) for _ in range(4)])


stats = ("str", "dex", "con", "int", "wis", "cha")
stat_dict = {}

for stat in stats:
    stat_dict[stat] = get_rolls()
print(f"###################################")
print(f"# Result  = d1  + d2  + d3 # cull #")
for stat, val in stat_dict.items():  # more convenient than doing stat_dict[stat] each time
    # instead of 4 manually interpolated parts, can use join:
    print(f'# {stat.upper()}: {val.sum:<3}=  {"  +  ".join(map(str,val.unculled))} #  ({val.cull}) #')
print(f"###################################")
narrow quailBOT
# dusky perch !e so I'd do something like this: ```py import random from dataclasses import da...

:white_check_mark: Your 3.12 eval job has completed with return code 0.

001 | ###################################
002 | # Result  = d1  + d2  + d3 # cull #
003 | # STR: 13 =  4  +  6  +  3 #  (1) #
004 | # DEX: 8  =  1  +  3  +  4 #  (1) #
005 | # CON: 7  =  1  +  2  +  4 #  (1) #
006 | # INT: 16 =  6  +  6  +  4 #  (3) #
007 | # WIS: 14 =  4  +  4  +  6 #  (1) #
008 | # CHA: 13 =  6  +  5  +  2 #  (1) #
009 | ###################################
narrow quailBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.