I have a couple of questions. The code below doesn't run because I've specified three arguments to the __init__
method and the make_dog
function returns a dictionary.
class Dog():
def __init__(self, name, colour, sex):
self._name = name
self._colour = colour
self._sex = sex
def __str__(self):
return '{}, {}, {}'.format(self._name, self._colour, self._sex)
def make_dog():
user_dog = {}
yes_or_no = input('Would you like to make a dog?! ')
if 'y' in yes_or_no:
print('Great! Let\'s get started')
user_dog['name'] = input('What shall we call the dog: ')
user_dog['colour'] = input('What colour is the dog: ')
user_dog['sex'] = input('What sex is the dog: ')
return user_dog
else:
print('Come back when you want to make a dog. Bye...')
exit()
d = Dog(make_dog())
print(d)
Here is the output:
Would you like to make a dog?! y
Great! Let's get started
What shall we call the dog: doggie
What colour is the dog: white
What sex is the dog: male
Traceback (most recent call last):
File "test.py", line 25, in <module>
d = Dog(make_dog())
TypeError: __init__() missing 2 required positional arguments: 'colour' and 'sex'
What would be the best/standard way to create a dog based on user input as I'm trying to achieve?
Is using dictionaries like in the
make_dog
function a recommended way of storing and returning values from a function? I think it is but want to check.
Many thanks.
EDIT: I don't think this is a duplicate of Passing a dictionary to a function in python as keyword parameters because I think my question is more beginner centric and specific. Hopefully some other beginner will find this question when wondering how to make an instance from using input()
.