Good afternoon, I sit on Py.Checkio, studying Python 3. Now the task of which you need to replace all the words Right on Left. I decided to go probably difficult way and decided to go through a lot of cycles. In short, here is my code:
def left_join (Phrases: Tuple) - & gt; Str:
result_str = ''
For CH in Phrases:
res_ch = str (CH) .split ('')
Length = Len (RES_CH)
For Char in Res_ch:
IF 'Right' In Str (Char):
START = STR (CHAR) .Find ('Right')
END = START + LEN ('RIGHT')
Result = Str (char) [: start] + 'left' + str (char) [end:]
If Length & GT; 1:
result_str + = result + ''
Length - = 1
ELSE:
IF LEN (Phrases) == 1:
result_str + = result
ELSE:
result_str + = result + ','
ELSE:
IF char == phrases [-1]:
result_str + = str (char)
ELSE:
result_str + = str (char) + ','
RETURN RESULT_STR
Problem start when it is trying to automatically substitute a tuple (“Lorem”, “Ipsum”, “Dolor”, “Sit”, “Amet”, “Consectetuer”, “Adipisting”, “ELIT”, “Aenean”, ” Commodo “,” Ligula “,” Eget “,” Dolor “,” Aenean “,” Massa “,” Cum “,” Sociis “,” Natoque “,” Penatibus “,” et “,” magnis “,” dis ” , “Parturient”, “Montes”, “Nascetur”, “ridiculus”, “mus”, “donec”, “quam”, “felis”, “ultricies”, “nec”, “petelntesque”, “eu”, ” Pretium “,” Quis “,” SEM “,” Nulla “,” Consequat “,” Massa “,” quis “).
He gives the words
“quis”
and
“SEM”
For some reason, as one whole, that is, ‘Quissem’.
I do not understand why, I tried to look at the visualizers and in different IDLE, I do not understand why he glues exactly these lines
Answer 1, Authority 100%
if char == phrases [-1]: # with char == "QUIS"
result_str + = str (char) # elements glued
ELSE:
result_str + = str (char) + ',' # otherwise separated by a comma
Here is your gluing, because the last element of the taper, i.e. phrases [-1]
you have "quis"
and therefore on the "quis" element
which is SEM in front of the
This if
is executed and the elements are not separated by the comma, unlike all other passes of the cycle on the elements of the price.