look, I am trying to make a code that has an input, when putting the input the code reads the input and has to find if it has an "a" or an "A" in the text, if so it gives an answer and if not give another The problem is that it doesn't matter if it has the letter A or not, only the if print is activated and not the else print.
print("introduzca su texto")
valido=False
Texto=Input()
for i in range(len(Texto)):
if Texto=="a" or "A":
valido=True
if valido:
print("Es valido")
else:
print("No es valido")
In the input it doesn't matter if I put A, Hello, B, a, or whatever it is that always says "It's Valid", the only time it didn't come out valid is when I hit enter with the blank text, that is without writing anything. The best thing is that I have a code exactly the same that only changes the text and the "a" or "A": for an "@":
The line
contains two errors: the first is that you compare all input against one letter, when you should be comparing only one letter.
The second is the
or "A"
. Your intention is to ask if itTexto
is equal to the lowercase "a" or the uppercase "A".Python doesn't interpret it that way. What comes after
or
is a logical expression , which is evaluated to determine its truth value. The expression in this case is a simple constant "A", which it isTrue
by definition.Therefore, what Python sees is
what always happens.
The correct form is
or alternatively
Other points:
print
with theinput
.for letra in texto
.break
to stop looking at the rest.Solution
Edition
The code can be even more compact:
Instead of going through the text and asking letter by letter, you simply ask if "a/A" is within the entered text.