Erik van Baaren

Forum Replies Created

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • in reply to: Python for-Loop #2344
    Erik van Baaren
    Keymaster

    OK sorry for the late reply. What you want is a range: https://docs.python.org/3.3/library/stdtypes.html?highlight=range#range

    E.g.:

    for i in range(1, 11):
        print(i)

    This will give the output you are looking for. The range() function is an iterable, more about that can be read here: https://python.land/deep-dives/python-iterator

    Alternatively, you can use a list of numbers. Something like for i in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]:.

    But the example I gave you used a string, and with the for loop you iterate over the letters. Python sees those as strings, not as numbers.

    in reply to: Python for-Loop #2339
    Erik van Baaren
    Keymaster

    I’d love to help, but you’ll need to fix the formatting or repost the question. This is unreadable ๐Ÿ™

    Erik van Baaren
    Keymaster

    So you want something like this, I think:

    import sys
    import os
    
    i =  (โ€œD90โ€)
    print(i)
    
    if i == (โ€œD90โ€):
        # it's true, so set led
    else:
        # it's false, set other led

     

    in reply to: Deploying Python (multi stage builds) #1928
    Erik van Baaren
    Keymaster

    You’re right. I’m actually working on a separate article about creating the ideal Dockerfile for Python projects. It includes multi-stage builds and picking the right image. Can’t say when it’s finished though.. I can’t work on this site full time, unfortunately ๐Ÿ™‚

    in reply to: Classes and Objects in Python #1914
    Erik van Baaren
    Keymaster

    I see what happened, the forum holds any post with a link for moderation (because of spammers, unfortunately). Looking at your screenshot, I can see two problems:

    * You’re mixing code in a file with the REPL
    * It looks like you didn’t save the file (control + s)

    Not sure what the reason is here, but I’d suggest staying in the REPL and getting it working there first before you start creating and using files.

    in reply to: Classes and Objects in Python #1911
    Erik van Baaren
    Keymaster

    Let me put this another way. In your code example, you let the definition of car directly follow the class definition. I mean the line car = Car().

    If you copy-paste or enter it like that in the REPL, it won’t work. You need to keep hitting enter after the class definition (effectively two times) until you get a new prompt (the >>>).ย  So if I do this on my pc, it looks like this:

    Python 3.8.5 (default, Jul 28 2020, 12:59:40)
    [GCC 9.3.0] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> class Car:
    ...     speed = 0
    ...     started = False
    ...
    ...     def start(self):
    ...         self.started = True
    ...         print("Car started, let's ride!")
    ...
    ...     def increase_speed(self, delta):
    ...         if self.started:
    ...             self.speed = self.speed + delta
    ...             print('Vrooooom!')
    ...         else:
    ...             print("You need to start the car first")
    ...
    ...     def stop(self):
    ...         self.speed = 0
    ...         print('Halting')
    ...
    >>> car = Car()
    >>> car.increase_speed(10)
    You need to start the car first
    >>>
    

    As mentioned below, I’d suggest staying away from files for now and try to get this working in the REPL first.

    Hope that clears it up!

    in reply to: Classes and Objects in Python #1894
    Erik van Baaren
    Keymaster

    I tried it myself to see if I can replicate your problem. What might be happening, is that you copied the class and didn’t hit enter twice after that. If you enter a multi-line statement, function, or class, you need to hit enter twice to finish it.

    Hope that helps!

    in reply to: Classes and Objects in Python #1891
    Erik van Baaren
    Keymaster

    Could you copy and paste the exact error for me?

    in reply to: Nested list comprehension examples #1670
    Erik van Baaren
    Keymaster

    Thank you for letting me know.

    There were two errors in the examples. First of all, the result of the first example should be assigned to a variable m. That variable is used in the next two examples.

    Second: there were too many block quotes in the first ‘flatten’ example. I fixed both errors, so please try again!

    in reply to: comparing digits and letters 1 >’a’ outputs an error #1664
    Erik van Baaren
    Keymaster

    That’s right!

    in reply to: comparing digits and letters 1 >’a’ outputs an error #1631
    Erik van Baaren
    Keymaster

    Hi Lu,

    Python tells you:
    โ€˜<โ€˜ not supported between instances of โ€˜intโ€™ and โ€˜strโ€™

    This means you can’t compare two different types in Python. You can compare numbers with numbers, and you can also compare letters with letters.

    I can see why you tried though since I gave an example where I did exactly that. I updated the tutorial page because I meant to give the example ‘1’ < 'a' instead of 1 < 'a'.

Viewing 11 posts - 1 through 11 (of 11 total)