Hey. Question | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
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

19th Jun 2019, 12:54 AM
Kidale Smith
Kidale Smith - avatar
6 Antworten
+ 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.
19th Jun 2019, 1:07 AM
Usama Bin Mamun
Usama Bin Mamun - avatar
+ 3
Kidale I didn't mean to make you look bad. Sorry 🙏 I think the proper term is conditional iteration (followed by addition).
19th Jun 2019, 9:48 PM
Usama Bin Mamun
Usama Bin Mamun - avatar
+ 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).
19th Jun 2019, 1:08 AM
Diego
Diego - avatar
+ 2
Usama Bin Mamun you wanna be my coach. 😁
6th Jul 2019, 2:45 AM
Kidale Smith
Kidale Smith - avatar
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)"
19th Jun 2019, 1:01 AM
Kidale Smith
Kidale Smith - avatar
0
Usama Bin Mamun why you gotta make me look bad 🤣, anyway. So what is the proper term if not indexing for this case?
19th Jun 2019, 5:47 PM
Kidale Smith
Kidale Smith - avatar