We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • Hackerrank Home
  • Prepare
    NEW
  • Certify
  • Compete
  • Career Fair
  • Hiring developers?
  1. Prepare
  2. Tutorials
  3. 30 Days of Code
  4. Day 4: Class vs. Instance
  5. Discussions

Day 4: Class vs. Instance

Problem
Submissions
Leaderboard
Discussions
Editorial
Tutorial

Sort 1721 Discussions, By:

votes

Please Login in order to post a comment

  • adelicatedegree
    6 years ago+ 18 comments

    This is just depressing. I'm old now :(

    402|
    Permalink
    View more Comments..
  • akshayiyengar
    6 years ago+ 43 comments

    After much banging of my head, I Googled "local variable referenced before assignment", because I kept getting the "UnboundLocalError: local variable 'age' referenced before assignment" error. One of the answers suggested I use 'global' for Python and it worked! The workaround is to pass a parameter instead of using the more dangerous 'global' keyword, but since we can't change the calling code, 'global' is the way to go. If someone solved this without using 'global' for Python3, please post your solution. It would be good to learn better ways.

    class Person:
        def __init__(self, initialAge):
            # Add some more code to run some checks on initialAge
            self.age = 0
            if initialAge < 0:
                print("Age is not valid, setting age to 0.")
            else:
                self.age = initialAge
    
    
        def amIOld(self):
            # Do some computations in here and print out the correct statement to the console
            if age < 13:
                print("You are young.")
            elif 13 <= age < 18:
                print("You are a teenager.")
            elif age >= 18:
                print("You are old.")
    
        def yearPasses(self):    
            # Increment the age of the person in here
            global age
            age += 1
    
    136|
    Permalink
    View more Comments..
  • imrans110
    5 years ago+ 8 comments

    Am I the only one who think this problem is poorly explained?

    69|
    Permalink
    View more Comments..
  • lordbell21
    6 years ago+ 28 comments

    The C# Problem is broken. It adds another newline at the end of the final test case, so it's impossible to match the expected solution.

    "Age is not valid, setting age to 0. You are young. You are young.

    You are young. You are a teenager

    You are a teenager You are old.

    You are old. You are old.

    "

    When it is saying it should be:

    "Age is not valid, setting age to 0. You are young. You are young.

    You are young. You are a teenager

    You are a teenager You are old.

    You are old. You are old. "

    I can't modify the main, so I'm stuck failing test cases.

    58|
    Permalink
    View more Comments..
  • Australianfrog
    6 years ago+ 2 comments

    But you're still a teenager if you're 18...

    19|
    Permalink
Load more conversations

Need Help?


View tutorial
View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature