0
Hey. Question
So I have this code. And it's simple and i figured it out. But when i added "+1" my answer is no loner 9, which is half of 18. Ita now 10. Why isn't it still 18? Can someone explain? Is it simply because by adding 1 I increased my index by 1, moving the 9 from the 9th spot to the 10th? Meaning even tho it's saying 10 the answer is still 9, just hidden in the 10th spot of my updated index? https://code.sololearn.com/cQuQOH6CXk13/?ref=app
6 ответов
+ 4
Hey Kidale
the index does not matter. Because this is not indexing.
If your code is like this,
sam = [i+1 for i in range(20) if i * 2 == 18]
print(sam)
then your code has successfully executed your command by finding the number whose double is 18 (i.e. the number 9), and then it added one to it. So, simply enough 9+1=10
And if your code is like this,
sam = [i for i+1 in range(20) if i * 2 == 18]
print(sam)
It will give you an error
I hope that answers your question.
+ 3
Kidale I didn't mean to make you look bad. Sorry 🙏
I think the proper term is conditional iteration (followed by addition).
+ 2
It has nothing to do with indexes. Take a look at this example.
sam = [i*3 for i in range(20) if i * 2 == 10]
print(sam) # [27]
If the condition is True then the number gets multiplied by 3 and the result is added to the list. The same thing happens with "i+1" (9+1 == 10).
+ 2
Usama Bin Mamun you wanna be my coach. 😁
0
Hey Diego. Well you can't tell here. But originally it said.
" I for I in range(20) "
I changed it to
" i+1 for I in range(20)"
0
Usama Bin Mamun why you gotta make me look bad 🤣, anyway. So what is the proper term if not indexing for this case?