Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

<__main__.myClass object at 0x000001CCDC46BD90> instead of expected string output

Write a Python class which has two methods get_String and print_String. get_String should accept a string from the user and print_String should print the string in upper case.

My Code:

class myClass():
    def __init__(self, userInput = input("What is your name? ")):
        self.userInput = userInput
    def get_string(userInput):
        return userInput
    def print_string(userInput):
        output = userInput.upper()
        return output
print(myClass())
Terminal:
What is your name? Anthony
<__main__.myClass object at 0x000001CCDC46BD90>

Should have come out as:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

ANTHONY

>Solution :

You seem to be missing the fundamentals re: classes in Python:

  1. The assignment asks you to get the string using the get_string method, not during __init__.
  2. Your print_string should print the stored string, it shouldn’t require the user to pass a new string.
  3. You are not attempting to convert the string to uppercase anywhere in your code.

Here is an improved version:

class MyClass:
    def __init__(self):
        self.name = None

    def get_string(self):
        self.name = input("What is your name? ")

    def print_string(self):
        print(self.name.upper())


my_object = MyClass()
my_object.get_string()
my_object.print_string()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading