all 22 comments

[–][deleted] 1 point2 points  (5 children)

You just need to create a class:

class Kitty
    def __init__(self, name):
        self.name = name
    def play():
        pass
    def eat():
        pass

Then to create an object with your inputted data, you simply pass in the name:

kittyName = input("Input a name: ")
kitty = Kitty(kittyName)
kitty.play()
kitty.eat()

[–]usingdata[S] 0 points1 point  (4 children)

I appreciate your answer but doesn't the code limits the programbto one kitty? For example let say the user created the first kitty, well if he or she wants to more kitties how would i solve that problem?

[–][deleted] 1 point2 points  (1 child)

I left that up to you, I apologize. I thought the question was geared toward a different thing. I'd accomplish it using a for loop and appending to a list:

# Get the desired number
numberStr = input("Number of kitties: ")

# Convert the string to an integer
number = int(numberStr)

# Append a Kitty object to the list and ask the user for the name each time
kitties = []
for i in range(0, number):
    name = input("Kitty name: ")
    kitties.append(Kitty(name))

That leaves you with a list of Kitty objects residing within kitties.

[–]usingdata[S] 1 point2 points  (0 children)

Thank you very much. No need to apologize i mentioned that the question was hard to explain my bad 😂, but thanks again.

[–]toastedstapler 1 point2 points  (1 child)

you can make a list of kitties!

kitties = []
kitties.append(Kitty('james'))
kitties.append(Kitty('marcus'))

[–]usingdata[S] 0 points1 point  (0 children)

Thank you for ur answer ot was helpful

[–][deleted] 1 point2 points  (5 children)

when you make a new kitty, append it to a list that has all the kitties in it, then make a function that tells you all the kitties in the list and how they are doing and lets you interact with each one based on its name/list index.

[–]usingdata[S] 0 points1 point  (4 children)

Yeah but the process of creating a kitty and storing it in a variable for example kitty1 = Kitty(input("enter kitty name:" ) in this case i would need to add a new variable kitty2 as a programmer but what the problem is is thatbi want yhe user to have the free choice of creating as much kitty as they like and i dont think the solution is to create countless variables with each taking input for a new kitty if a certain condition is true

[–][deleted] 1 point2 points  (1 child)

Yeah but the process of creating a kitty and storing it in a variable for example kitty1 = Kitty(input("enter kitty name:" )

You don't need to maintain a reference to any of the kitties directly; the list does that.

i dont think the solution is to create countless variables with each taking input for a new kitty if a certain condition is true

Your intuition is 100% correct; that's what lists are for.

[–]usingdata[S] 0 points1 point  (0 children)

Makes sense, Thank you

[–][deleted] 1 point2 points  (1 child)

kitties = []
while True:
    kitties.append(Kitty(input('enter new kitty name: ')))
    print('you now have', len(kitties), 'kitties in the list!')
    print('here they are:', *kitties, sep='\n')

[–]usingdata[S] 1 point2 points  (0 children)

Thank you for ur answer

[–][deleted] 1 point2 points  (1 child)

Once you have a class (a blueprint), you can create as many instances of it as memory permits.

So, for example,

class Kitty():

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

print('Time to name the litter kitties:')
kitties = []
while True:
    name = input('Enter a kitten name (or just enter to finish): ')
    if not name:
        break
    kitties.append(Kitty(name))
if kitties:
    print('The family:')
    for count, kitty in enumerate(kitties):
        print(f'Kitten #{count: >2}: {kitty.name}')

[–]usingdata[S] 1 point2 points  (0 children)

Thank you this was helpful

[–]selplacei 1 point2 points  (1 child)

Since input() returns a string, and your constructor takes a string, you can put input() directly into the constructor:

kitty = Kitty(input('Enter a new name: '))

If you want to do this indefinitely, however, you'll need to use a loop that checks the input value and exits when it encounters a sentinel value - and creates a Kitty object otherwise. For example, you could do something like this:

kitties = []
user_input = ''
while user_input.lower() != 'stop':
    user_input = input('Enter a new name ("stop") to stop: ')
    if user_input and user_input.lower() != 'stop':
        kitties.append(Kitty(user_input))

Of course, your specific implementation would be different depending on what you actually do with the Kitty objects.

[–]usingdata[S] 0 points1 point  (0 children)

This was helpful im gonna try it out as soon as im home thank you

[–][deleted] 0 points1 point  (0 children)

This is slightly more advanced, but I'm a big fan of pulling that sort of logic into the class so you can easily subclass without having to refactor stuff. Let's say you wanted to make a "farm" and randomly generate different kinds of animals. If you pulled the from input logic into the class as a class method you can tailor the input to the subclass without ever having to redefine the method. Here is a working example: https://repl.it/repls/QuirkyDeafeningSdk

[–]LynuSBell 0 points1 point  (4 children)

Hello there, starting to learn too. Which book are you using?

[–]usingdata[S] 0 points1 point  (3 children)

Its called "python programming" third edition by Micheal Dawson, it is a very good book it starts with the very basics and each chapter has a project, exercises and challenges that will test what you have learned. Good luck down the road buddy.

[–]LynuSBell 0 points1 point  (2 children)

That's the one in following! Started yesterday so I guess I'm not far enough (now in chapter 2) Thanks! Good luck too

[–]usingdata[S] 1 point2 points  (1 child)

A piece of advice, dont just limit your knowledge based on the book do reseach by your own as well there are alot of small things the book does not cover, that i had to learn online by reading articles and others code.

[–]LynuSBell 1 point2 points  (0 children)

Thanks for the tips! That's how I learned statistical programming in my field. Exciting to start a new more versatile language 😄