Forum Replies Created
OK sorry for the late reply. What you want is a range: https://docs.python.org/3.3/library/stdtypes.html?highlight=range#range
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.
I’d love to help, but you’ll need to fix the formatting or repost the question. This is unreadable 🙁April 1, 2021 at 10:25 pm in reply to: I need to get a True or False if a variable contains a string match #2314
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
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 🙂
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.
Let me put this another way. In your code example, you let the definition of
cardirectly 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!
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!
Could you copy and paste the exact error for me?
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!February 2, 2021 at 3:53 pm in reply to: comparing digits and letters 1 >’a’ outputs an error #1664
That’s right!January 30, 2021 at 9:29 am in reply to: comparing digits and letters 1 >’a’ outputs an error #1631
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'.