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 6911707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:59:29+00:00 2026-05-27T08:59:29+00:00

i was trying to write a class for a little program i’ve been working

  • 0

i was trying to write a class for a little program i’ve been working on, and i ran into an odd failure of indenting. I was wondering if there’s genuinely something wrong with what i wrote, or if it’s the parser’s fault. This fails in Fedora 15’s python 3.2 package.

  def __getitem__(self, key):
    if CharacterStats.usesSubStats:
      if key in self.subStats:
        return self.subStats[key]
    elif key in self.stats: #change this to 'if' and it works
      return self.stats[key]
    else:
      raise KeyError(key)
    #otherwise we end up right here.

As requested so you can run it: http://pastebin.com/d8yQUm3U

  • 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-05-27T08:59:30+00:00Added an answer on May 27, 2026 at 8:59 am

    If I understand you correctly, you want to throw a KeyError if
    usesSubStats is True and key is not in subStats or if usesSubStats is
    False and key is not in stats. So I think the problem is that
    if/elif/else chaining doesn’t work like you think it does.

    Consider:

    def f(x):
        if x == 1:
            return 'first'
        elif x == 2:
            return 'second'
        else:
            return 'other'
    

    produces

    >>> f(1), f(2), f(3), f(4)
    ('first', 'second', 'other', 'other')
    

    which I hope does what’s expected and is the pattern you should keep in mind. Since in your test code useSubStats is True, only the first branch is ever tested:

    def condition(lab, val):
        print('testing condition', lab);
        return val
    
    def g():
        if condition(1, True):
            return 'first branch'
        elif condition(2, False):
            return 'second branch'
        else:
            return 'other branch'
        return 'fallthrough'
    
    
    >>> g()
    testing condition 1
    'first branch'
    

    So your code is behaving like this:

    def h():
        if condition(1, True):
            if condition('1b', False):
                return 'first branch'
        elif condition(2, False):
            return 'second branch'
        else:
            return 'other branch'
        return 'fallthrough'
    
    >>> h()
    testing condition 1
    testing condition 1b
    'fallthrough'
    

    I’m not exactly sure how you think it should be behaving, but it seems like you expect that after the “if key in self.subStats” test fails, execution should move back to the next member of the if/elif/else branch one level up and test that. That’s simply not how it works, though.

    There are several easy ways to get the behaviour you want: one is to flatten it so that it’s

        if CharacterStats.usesSubStats and key in self.subStats:
    

    instead, which will evaluate to False and so the next branch will be tested, or –as you discovered yourself — make the elif an if, which again leads to that condition being independently tested, or rewrite as in an earlier answer.

    Does that make sense? The if/elif/else list describes a series of possibilities, with conditions tested sequentially, and only the branch corresponding to the first true condition (taking the final else as ‘elif 1:’) is executed. You don’t move to the next branch depending on what happens within a branch.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a program that accepts type comparable into the DataSet class
I've been trying to write a program in R that implements Newton's method. I've
I'm trying to write a little program that will add mDNS CNAME aliases to
I'm trying to write a program that is just a little beyond my abilities.The
I am trying to write a little Ruby program that can insert two letters
I'm trying to write a little C# program that reads from a text file
I am trying to write a program for class that reads a list of
I'm trying to write a class in Delphi 2007 that uses a ActiveX library.
I'm trying to write a small class library for a C++ course. I was
I am trying to write a Java class to extract a large zip file

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.