Sep-24-2020, 01:10 PM
Here is what the PyBite #181 exercise is calling for:
I solved the problem (see script below). To come up with my script, I leveraged Python.org’s bisect doc.
The solution I wrote passes all the unit tests but as a ‘victory lap’ I’m exploring how to call classes in general within my script using my trusty REPL and I am doing something wrong because my output doesn’t process as expected.
Here is the script I am working with:
How do I achieve the expected output in my REPL? How do I instantiate and properly pass in integers on the fly in my REPL?
I realize in the PyBite example REPL commands use:
Quote: Complete the add method of the OrderedList class which takes a num argument and adds that to the self._numbers list keeping it ordered upon insert.
Using a manual .sort() or .sorted() each time is not allowed. Look into the bisect module how to do it ...
I solved the problem (see script below). To come up with my script, I leveraged Python.org’s bisect doc.
The solution I wrote passes all the unit tests but as a ‘victory lap’ I’m exploring how to call classes in general within my script using my trusty REPL and I am doing something wrong because my output doesn’t process as expected.
Here is the script I am working with:
import bisect class OrderedList: def __init__(self): self._numbers = [] def add(self, num): bisect.insort_right(self._numbers, num) def __str__(self): return ', '.join(str(num) for num in self._numbers)In my script’s directory, I activate my REPL, import the script and instantiate:
$ bpython bpython version 0.19 on top of Python 3.8.5 /usr/bin/python >>> import order >>> dummy = order.OrderedList >>>Here are some inputs with my expected outputs:
dummy.add(10) print(dummy) 10 dummy.add(1) print(dummy) 1, 10 dummy.add(16) print(dummy) 1, 10, 16 dummy.add(5) print(dummy) 1, 5, 10, 16But here is my actual output:
>>> dummy.add(10) Traceback (most recent call last): File "<input>", line 1, in <module> dummy.add(10)The AttributeError says that the module (order.py) does not contain an attribute (a variable within a class) but as you can see in my script, an attribute is clearly declared at line 7. I believe the problem is not with my script, but with how I am instantiating it in my REPL.
How do I achieve the expected output in my REPL? How do I instantiate and properly pass in integers on the fly in my REPL?
I realize in the PyBite example REPL commands use:
order = OrderedList() order.add(10)That seems to work. But when I replace 'order' with 'dummy' (because I am experimenting with an alternate variable name) the output falls apart.