Closed smithjackson35 closed 5 years ago
can you share your actual .py file?
or just paste the code for it in a github gist
(base) C:\Users\johndoe>ipython Python 3.7.3 (default, Apr 24 2019, 13:20:13) [MSC v.1915 32 bit (Intel)] Type 'copyright', 'credits' or 'license' for more information IPython 7.6.1 -- An enhanced Interactive Python. Type '?' for help.
In [1]: conda activate dsfs
Note: you may need to restart the kernel to use updated packages.
In [2]: class CountingClicker: ...: """A class can/should have a docstring, just like a function""" ...:
In [3]: def init(self, count = 0): ...: self.count = count ...:
In [4]: clicker1 = CountingClicker() #initialized with 0
TypeError Traceback (most recent call last)
the problem is with the way you're using IPython console. Whenever you put in two blank lines, that tells it that you're done giving it code and that it should run what you've given it. So, for example, when you do:
In [2]: class CountingClicker:
...: """A class can/should have a docstring, just like a function"""
...:
In [3]: def __init__(self, count = 0):
...: self.count = count
...:
in the first you're defining an empty class and then saying "I'm done with this class", and in the second you're defining a bare function that (now) has nothing to do with the previous class.
to do this kind of thing in IPython console you need to not use blank lines:
In [1]: class CountingClicker:
...: def __init__(self, count=0):
...: self.count = count
...:
In [2]: CountingClicker()
Out[2]: <__main__.CountingClicker at 0x7f1264413ac8>
In [3]: CountingClicker(10)
Out[3]: <__main__.CountingClicker at 0x7f12643c40b8>
or else use the %paste
magic:
https://stackoverflow.com/questions/10886946/how-does-ipythons-magic-paste-work
Awesome! Thanks for your help.
I am relatively new to Python and am having issues building the counting clicker on page 31-32.
I start with the
then I try to initialize the clickers
I get the error message that counting clicker takes no arguments.
I though at first I replaced "init" above with "CountingClicker" but that did not work either and results in the same error message.
Can someone please help I am struggling with learning classes and would really appreciate it. Let me know if I need to provide my actual .py file.