Python - attempting to make a morse code LED [duplicate] - python

This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 7 years ago.
i have attempted to write the code such that it will tell you to input your string. at which point you type a string for example lets say you type "purple" then press enter. the code will run and get the length, then instead of getting the first letter variable[0] as P and then lighting the LED in the code for P, it constantly interprets all letters as A. i know its going to be something small but ive only been learning python for a few days so try to be nice. i have previous experience with VB so i have most likely used the wrong syntax somewhere but i cant figure it out. PLEASE HELP.
i cant copy the code into this without rewriting it so im going to use screenshot links.
this is where i think the problem must be
http://puu.sh/lUf9q/3ad50c4faf.png
and then this is how i've set the morse code patterns
http://puu.sh/lUfgo/f6f3f2cb32.png
the reason there is 2 = and 1 == is because i was tinkering with them to see if they were the problem which they aren't
Thanks in advance
EDIT:
apparently i have to edit it to say why its different to said thread. The way its different is: its not but I didn't realise that the problem in the thread was the problem i was having.

It is interpreting all letters as "a" because your if statement says
if curr = "A" or "a":
In fact == and = are different. One does assignment, and the other checks for equality. This if statement always executes because there is always one side of the or that is true.
Try this instead
if curr.lower() == "a":

Related

Can you please explain me this for loop of a python quiz question? [duplicate]

This question already has answers here:
Behaviour of increment and decrement operators in Python
(11 answers)
Closed 1 year ago.
I was surfing YouTube and found a random video of a python quiz. The question goes like this:
What will be the output of this python code:
a = []
for i in range(10):
a.append(i * ++i)
for a[i] in a:
print(a[i])
I paused the video, tried to figure out the output, but I couldn't, because python doesn't have any ++ operator. Then I continued the video and I understood the first for loop, but the second for loop is very tricky, as is mentioned in the video. The guy explained in the video what is happening in the second for loop, but the explanation was too brief for me to understand it clearly. I tried to run the code in VS code, trying print statements and other stuff but still no help.
So, can you please help me in understanding the second for loop, maybe a step-by-step explanation?
Here's the video link (Hindi language): https://youtu.be/HbjaAm4Bib0
You can run it to see the output. The code is misleading and error prone (was that a quiz on bad practices?) .
The i * ++i part seems to suggest some special additive process for the ++i but ++ is not an operator in Python and it is simply interpreted as indicating that the sign of i is unchanged twice (in short, ++ does nothing here so the code is equivalent to i*i)
The for a[i] in a: part is really bad. It uses the residual value of i (which is the last index of a in this case) and makes that item in a the iteration variable for the loop. This means that at every iteration, the last item in a will be assigned with the current item value. This will cause the last value printed to be the same as the one-before-last (thus losing the last value in a)

Python check if a variable appears in another variable [duplicate]

This question already has answers here:
Python efficient way to check if very large string contains a substring
(8 answers)
Closed 1 year ago.
I'm a beginner to Python. I'm using the request module to get the text from a website that contains blacklisted users for the login system of my program. I want to know how to check if a variable appears in another variable such as, "if variable appears in variable2: do something"
Can anyone help? Thanks.
You can check that using the in keyword -
if object1 in object2:
#do something
Share your code. It would give a better understanding of what you need to do. I think the below code will work.
import requests
x = requests.get('https://yourwebsite.com')
if variable in x.text:
#do something

I got something wrong with the else statement [duplicate]

This question already has answers here:
Why does "a == x or y or z" always evaluate to True? How can I compare "a" to all of those?
(8 answers)
Comparing a string to multiple items in Python [duplicate]
(3 answers)
Closed 2 years ago.
Note: I'm not a fluent english speaker, so you may see some grammar mistakes here. I'm kinda new to programming and i know i can fix it by putting another else/elif statement, i just trying to get the short possible lines to avoid spamming elif like yandev.
so the thing is, at the #functions i already put if below the gae variable's input, which says that if the input = y or Y then it runs the line below it, which is print("good. go to www.minecraft.net"). Now my problem is, when i try to input other letter than Y, it still runs the same line whilst there's already an else line underneath that i think should exclude other words than Y or y. For example, i already input the word "n" at the output console(?) but the output says "good. go to www.minecraft.net" instead the lines at the else statement. is there any possible solution without adding much more elif/else statement?
Click this link for the image to understand what am i talking about
The line if gae == "Y" or "y" should be if gae == "Y" or gae == "y" otherwise it will always be true and the code will always print "good. go to www.minecraft.net".

Does "i" in a for loop default to a value of 1 in Python? [duplicate]

This question already has answers here:
Why can't "i" be manipulated inside for-loop [duplicate]
(3 answers)
Closed 3 years ago.
I just have a basic question about a for loop in Python 3. It's a stupid, simple question, but I figure everyone else here is far more knowledgeable to me, and most answers are pretty bright! I do not profess to be intelligent, so I'd like some clarification. Running this loop seems to add a value of only one. I have defined x to be 1, so this makes sense, but I didn't give i a true value and it seems to just naturally be 1.
for i in range(0, 50):
x = 1
i = x + i
print(i)
I'm not getting an error, I'm just curious as to what's going on behind the scenes. Would love some explanation from someone who understands this more than I do!
You do not need the x=i and i=x+i inside your loop.
for i in range(0, 50):
print(i)
would work just fine and would output 0,1,2,3...47,48,49. i is being set according to the range(0,50). If you want i to start at 1 simply change the range to range(1,50). Your code also added 1 to each i value so in order to account for this in the range you would need to do range(1,51) which would print 1,2,3...48,49,50.
Further Reading
I would recommend reading these to better understand how for loops work and also more info about ranges:
Range()
For Loops

Python, Having problems with .append() method [duplicate]

This question already has answers here:
How to modify list entries during for loop?
(10 answers)
Closed 5 years ago.
I'm using anaconda Python 3 notebook. And When I Try to append anything to a list, my pc goes mad. It gets slow, the RAM goes to 95% and it just wont work anymore. But i noticed somethin, the problem only occurs when I use a for statement. and if I use slice brackets I dont have that problem, so thi would be like this:
Problem:
for element in anylist:
anylist.append('whatever')
(So far, i think this one never stop working and it may be causing some truobles. I dont really know)
No problem:
for element in anylist[:]:
anylist.append('whatever')
Another detail: All this started right before I imported the String module, and the Os module. But now it happens every time i write single code.
Python is in 64 bits as it has to be in my case.
If you could help me I would appreciate it.
The first example can be translated as:
while there is something more in anylist add whatever to the end
which means the list will grow until the system crashes.
So it will never end. The second translates as:
for the number of items in anylist add whatever to the end of the list.
So will double the length of the list.
Hence python is doing exactly what you are telling it to do, (which I suspect is not what you think you are doing).
Try doing:
for element in range(len(anylist)):
anylist.append('something')

Categories

Resources