How do I define a variable with 2 outputs in Python?

Question:

I have this problem that I can´t solve.
I have this function:

def ex2(tex_ori):
    let = ("a" or "A")
    text_troc = tex_ori.replace(let, "x")
    return text_troc

And the execution for this is:

text_ori = input("Write a sentence: ")
     text_1 = ex2(text_ori)
     print(text_1)

But the only letter being replaced is just "a".
What am I doing wrong and what should i do?

I am expecting that all letters "a" and "A" are replaced with the letter "x".
For an example:
Sentence: Hi my name is Alex.
The return should be: Hi my nxme is xlex.

Thanks for the attention and help.

Asked By: Duarte Eusébio

||

Answers:

Your understanding of the line let = ("a" or "A") is incorrect.
In Python, in this context the or means: "Take ‘a’, or, if it is None, take ‘A’".
As ‘a’ is impossibly None, it will always be taken.

I think, what you are rather looking for is something like this:

let = ["a", "A"]
for l in let:
   tex_ori = tex_ori.replace(l, "x")
return tex_ori

Answered By: Christian

When you write let = ("a" or "A") the let variable will store only the first true statement from the two (in this case "a"). In order to replace both letters you can write it for example this way:

def ex2(tex_ori):
    text_troc = tex_ori.replace("a", "x")
    text_troc = text_troc.replace("A", "x")
    return text_troc

text_ori = input("Write a sentence: ")
text_1 = ex2(text_ori)
print(text_1)
Answered By: Petr Hofman
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.