+ 2
[Solved] Is there a way to get rid of one of the two identical statements in this python code?
Hi, I am sure there are many and much better ways to do the Secret Message exercise in code coach but this is the one I could come up with and I am wondering how I can simplify my own code if possible (the ntx augmented assignment repeated, the ch do not stand for the same thing but is there a rearrangement where I could use it only once?) https://code.sololearn.com/ckWfdFR9k5m8/?ref=app
11 Antworten
+ 3
Korkunç TheTerrible ,
here some other thoughts to the secret message task:
tx = input().lower()
alph = 'abcdefghijklmnopqrstuvwxyz'
ntx = ""
for ch in tx:
if ch in alph:
ntx += alph[-(alph.index(ch)+1)] # (1) (2)
print(ntx)
# (1) to get the new index for the reversed character, we use the index number that we get from the index() function, add + 1 to it, and make the number a negative number. so we are useing negativ index numbers that start from the end of the sequence.
# (2) it is also recommended to avoid [::-1] in this case, which is reversing the input sequence. reversing is performed each time a new iteration starts in the for loop.
+ 2
Try one of these codes. You will understand what I meant
mes = input().lower()
alpha = " abcdefghijklmnopqrstuvwxyz "
for i in mes:
print(alpha[::-1][alpha.index(i)],end="")
Or
[print(alpha[::-1][alpha.index(i)],end="") for i in mes]
+ 1
Nevermind someone helped me out, I got an answer, I'll ask to post it here
My indentation for the first one should be pulled back once to get rid of the excess under the else.
+ 1
FF9900 Thanks a bunch!! :-))
+ 1
Simba But the input might have space in it. We are only to change the members of the alphabet and not touch the space in between
+ 1
Simba Yes, but if I do that, it, too, will go in the for loop. And when it does, I will have to write extra code for the space character to be exempt from the loop operation. I could insert space as a character in the middle so that it doesn't change when the reversed string is being searched for the letter of the iterated index. It would be the 14th item from both ends, but the indexing starts from 0 and - 1 respectively which then would have required more code. I might be wrong but I'd thought about it earlier and refrained from doing so for this reason.
+ 1
Lothar I remember that that line was in my first code, although I think I did [-alph.index(ch)-1], I mean I didn't use the paranthesis inside the brackets, but I had other problems in that code and I rewrote it altogether. I was assuming that the reversed one would be preferable, because it is more understandable.
I am really happy to have posted this question and gotten your reply, because you informed me about something more important; your explanation in part two. Copying your text right into my notes.
I really want to learn efficient coding but I am a noob, and this type of info is hard to come by for a noob.
Thank you.
0
You don't need to use if statement as well.
for ch in tx:
print(alph[::-1][alph.index(ch)],end="")
Using list comprehension
[print(alph[::-1][alph.index(ch)],end="") for ch in tx]
0
Simba
1. Sorry I misclicked (tick instead of arrow, but I had already given it, clumsy fingers I should scroll better)
2. Sorry for being such a doofus and not getting it the first time, but I got it, thank you.
My phone's been acting up-delays, my apologies for misclicking too many times