Search code examples
pythonclassuser-input

Beginner Python Classes - changing the attribute value with user input


I am just learning classes in Python and for the past day I am stuck with the below.

I am trying to use a user input (from the main() function) to change the value of an attribute in the class.

I have been throught the @property and @name.setter methods that allow you to change the value of a private attribute.

However I am trying to find out how you can use user input to change the value of an attribute that is not private.

I came up with the below but it does not seem to work. The value of the attribute remains the same after I run the program. Would you have any ideas why?

    class Person(object):

    def __init__(self, loud, choice = ""):
        self.loud = loud
        self.choice = choice

    def userinput(self):
        self.choice = input("Choose what you want: ")
        return self.choice

    def choiceimpl(self):
        self.loud == self.choice

    def main():

        john = Person(loud = 100)

        while True:

            john.userinput()

            john.choiceimpl()

            print(john.choice)
            print(john.loud)

    main()

Solution

  • In choiceimpl you are using == where you should use =.