Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9149121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:26:03+00:00 2026-06-17T11:26:03+00:00

Update Someone is probably going to drop the hammer on me for posting this

  • 0

Update
Someone is probably going to drop the hammer on me for posting this way but there’s not enough room in comments to cover this and they specifically tell you not to answer your own question with a follow-up, so here goes…

I’ve created the dice class like you guys were talking about.

class dice():
    def __init__(self, sides, number):
        self.sides = sides
        self.number = number

    def roll(self):
        return random.randint(1, self.sides)

The number argument isn’t doing anything though.

def att():
    d = dice(20, 2)
    base = d.roll()
    if base == 1:
        print 'Miss!'
    elif base == 20:
        crit = d.roll()
        if crit < 10:
            print 'Hit!'
        else:
            print 'Critical hit!\n'
            effect = super_crit()               
    else:
        print base

The only way I’m getting more than one die is if I do something like this:

def initiative():
    d = dice(10, 1)
    ini = d.roll(), d.roll()
    print ini

I’ve also tried:

def initiative():
    d = dice(10, 2)
    d.sides = 10
    d.number = 2
    ini = d.roll()
    print ini

That looks redundant to me but I get a Type Error: for having too few arguments without dice(10, 2). No matter which method I use, I get the same result – one die. Am I missing something?

Original post

I’m learning how to use classes in Python 2.7 and as an exercise I’m writing a combat module for a text-based RPG. It uses the old school dice roll method to determine outcomes and effects. A roll determines a hit or miss. If a natural 20 is rolled, another roll determines if it was a critical hit. If critical hit = TRUE, another die is rolled to determined which body part is effected. Each body part is paired with a number 1-12 in a dictionary. There are three possible output messages depending on the affected part. My problem is that the entire list of values is returned instead of a specific part. What am I doing wrong here?

Yes, I know this is super nerdy. Yes, I know the output is lame, but it’s all place holder.

import sys, random

#dice generator
class dice():
    def d4(self):
        number = random.randint(1, 4)
        return number

    def d6(self):
        number = random.randint(1, 6)
        return number

    def d10(self):
        number = random.randint(0, 9)
        return number

    def d12(self):
        number = random.randint(1, 12)
        return number

    def d20(self):
        number = random.randint(1, 20)
        return number

    def d100(self):
        number = random.randint(0, 99)
        return number 

#critical hit effect generator        
class super_crit(dice):
    def __init__(self):
        roll = dice()
        loc = roll.d12()
        hit_loc = {1 : 'Head',
                   2 : 'Left Arm',
                   3 : 'Right Arm',
                   4 : 'Left Leg',
                   5 : 'Right Leg',
                   6 : 'Left Hand',
                   7 : 'Right Hand',
                   8 : 'Left Foot',
                   9 : 'Right Foot',
                   10 : 'Chest',
                   11 : 'Stomach',
                   12 : 'Body'}
        part = hit_loc.values()
        for w in part:
            if loc <= 9:
                print w, "has been severed!"
            elif loc == 10:
                print "You sink your blade into his", w, "and pierce the heart!"
            elif loc == 11:
                print "You slash him across the", w, "and eviscerate him!"
            elif loc == 12:
                print "You shred the enemy's", w, "to ribbons!"

class attackRoll(dice):
        pass

#Attack function        
def att():
    roll = attackRoll()
    base = roll.d20()
    if base == 1:
        print 'Miss!'
    elif base == 20:
        crit = roll.d20()
        if crit < 10:
            print 'Hit!'
        else:
            effect = super_crit()               
    else:
        print base

def main():
    att()


if __name__ == '__main__':

main()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T11:26:04+00:00Added an answer on June 17, 2026 at 11:26 am

    You loop over all the values of the dict:

    part = hit_loc.values()
    for w in part:
        # loop over each part, so print a damage message for all 12 bodyparts
    

    Perhaps you meant to pick the one that is affected instead?

    part = hit_loc[loc]  # assign *one* body part to `part`
    if loc <= 9:
        print part, "has been severed!"
    elif loc == 10:
        print "You sink your blade into his", part, "and pierce the heart!"
    elif loc == 11:
        print "You slash him across the", part, "and eviscerate him!"
    elif loc == 12:
        print "You shred the enemy's", part, "to ribbons!"
    

    In other words, you do not need a loop here at all.

    Note that, whenever you find yourself using a sequential series of numbers as keys to a dictionary, you may as well make it a list instead:

    hit_loc = [
        'Head', 'Left Arm', 'Right Arm', 'Left Leg', 
        'Right Leg', 'Left Hand', 'Right Hand', 'Left Foot', 'Right Foot',
        'Chest', 'Stomach', 'Body'
    ]
    

    except now the indexes run from 0 to 11, so use loc - 1 to find the right body part:

    part = hit_loc[loc - 1]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There's probably a better way to do this, but this is what I'm trying
Could someone tell me why this code does not update the data response? It's
There is probably a simple answer to this that someone can help me with
I have a table that someone update this table every day. I would like
Update: Is there a way to achieve what I'm trying to do in an
This is probably something really stupidly simple.. I have a drop down list bound
I am probably missing something obvious with this problem, but i can't seem to
All, This question probably has a very simple answer - something I'm overlooking. But
heyy guys.... i know this is probably a silly error but i really tried
Probably this is a basic stuff, but better learn now then later. What i'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.