Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
__init__ with input()
#1
I wrote this program and it works like I want it too but the __init__ method doesn't do anything. I have read and watched tutorials but I can't figure out how to apply the __init__ method with the input() function. How do I make __init__() more than just decoration and still get the same results? Confused
class Person():

    def __init__(self, name, age):
        self.name = name
        self.age = age

    def get_name():
        """Get the user's name."""
        enter_name = input('Enter your name. ')
        return enter_name
        

    def get_age():
        """Get the user's age."""
        enter_age = input('Enter your age. ')
        return enter_age

def main():
    name = Person.get_name()
    age = Person.get_age()
    print(f'My name is {name}. I am {age}.')

if __name__ == "__main__":
    main()
Output:
Enter your name. micah Enter your age. 37 My name is micah. I am 37.
Reply


Messages In This Thread
__init__ with input() - by mcmxl22 - Mar-30-2019, 05:43 PM
RE: __init__ with input() - by Larz60+ - Mar-30-2019, 05:58 PM
RE: __init__ with input() - by DeaD_EyE - Mar-30-2019, 06:04 PM
RE: __init__ with input() - by snippsat - Mar-30-2019, 09:16 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Why is there an __init__ statement within the __init__ definition iFunKtion 7 6,152 Feb-06-2017, 07:31 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020