hello can someone please explain this code for me ! thank you 🤗 | Sololearn: Learn to code for FREE!
¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita
+ 6

hello can someone please explain this code for me ! thank you 🤗

def f(r) : j =range(r) e=eval("*".join([str(i+2)for i in j])) return e print(f(3))

15th Sep 2018, 12:55 AM
molly
11 Respuestas
+ 16
The last line has an error, too many parentheses ;) Basically what the code does is it takes an input of 3 and: - builds a range of 0, 1, 2 - iterates through it and builds on-the-fly a list of 0+2, 1+2, 2+2 (so 2, 3, 4) - the str() method converts those numbers into strings and join() makes them glued together with an asterisk sign between the numbers (2*3*4) - finally eval() evaluates the expression (2*3*4=24) - the result is printed to the screen
15th Sep 2018, 7:06 AM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 7
thank you so much 😃Kuba Siekierzyński
15th Sep 2018, 7:55 AM
molly
+ 7
no one The problem is with the line 2, not 3. You have to close the parenthesis. Or as a matter of fact, dispose of str() altogether. Input() already returns a string ;)
17th Nov 2018, 7:17 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 6
Got it 😃 thank youuu 💛
29th Sep 2018, 7:04 PM
molly
+ 6
hh I do need some coffee 😅 thank youuu sir 💛❤
17th Nov 2018, 7:21 PM
molly
+ 5
mrr Kuba Siekierzyński can you please figure out with me whats wrong with line 3 !! its not an invalid syntax tho msg=str(input("saisir votre message:")) cle=str((input("saisir votre cle:")) LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' res='' i=0 for char in msg: begin=LETTERS.find(cle[i]) pos=LETTERS.find(char) indice=pos+begin if indice>25: indice-=26 res+=LETTERS[indice] i+=1 if i>=len(cle): i=0 print(res)
17th Nov 2018, 5:14 PM
molly
+ 4
The loop starts with i == 1. At first go it prints i+1, so 2. Then i itself is multiplied by 2 and is now 2. The loop goes off again and prints i+1. Since i is now equal to 2, i+1 is 3 and this one is printed. Meanwhile i is again multiplied by 2 and is now equal to 4. The loop's next cycle prints 4+1, so 5 and multiplies i by 2 again (i is now equal to 8. The last cycle prints out 8+1, so 9 and multiplies i by 2 again. Since i is now 16, so greater than 10, the loop ends. in fact, it's easier to think of this as a cycle similar to 1, 2, 4, 8, etc. BUT +1 :)
29th Sep 2018, 6:26 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 3
mr Kuba Siekierzyński would u please explain this one too ! didn't understand the principe ! i=1 while i<=10: print(i+1) i*=2 >>>2359 and really thanks a lot in advance 💛💚
29th Sep 2018, 4:47 PM
molly
0
hallow guy
1st Oct 2018, 5:13 PM
Haroun Adam
0
yes get me
17th Nov 2018, 9:38 PM
Haroun Adam
0
molly mr Kuba Siekierzyński would u please explain this one too ! didn't understand the principe ! i=1 while i<=10: print(i+1) i*=2 >>>2359 In first execution of while loop. It's true with i=0, then print i+1, i.e. equal to 2. Here print function just print without moving cursor to next line. Before completing first loop 'i' has been modified by i*=2 (i=i*2). so finally after completing first loop output (print) is 2 and i is 2. It'll iterate similarly in the next loops until the value of i will satisfy the while loop condition i<=10 In 2nd loop i=2, while condition=true, output=3 (on the same line), modified i=4 In 3nd loop i=4, while condition=true, output=5 (on the same line), modified i=8 In 4nd loop i=8, while condition=true, output=9 (on the same line), modified i=16 In 5nd loop i=16, while condition=false, loop will break and execute the next consecutive statements. output=nothing
15th Dec 2019, 5:22 AM
Nitesh Solanki
Nitesh Solanki - avatar