+ 1

Help me to make my code shorter !

I have just finished a code. Its done and giving me the correct output as I expected. but I thought if I can make these codes shorter. can you help me to convert this code into shorter?? https://code.sololearn.com/cAYdLG2Ga8Ne

26th Mar 2020, 4:21 PM
Md. Mursalatul Islam Pallob
Md. Mursalatul Islam Pallob - avatar
17 Antworten
+ 13
You could do it like this: fib1 = 0 fib2 = 1 sum_ = 0 while fib2 <= 4e6: if fib2 % 2 ==0: sum_ += fib2 fib1, fib2 = fib2, fib1+fib2 print(sum_) Ignore the shorter names, I only did that for readability. In your code, you had a while abort condition AND a break condition. One suffices. Also you can change the old values to the new ones in one line.
26th Mar 2020, 4:36 PM
HonFu
HonFu - avatar
+ 5
I'd like to suggest one thing: Let's not add all sorts of totally different solutions, but rather concentrate on improving on the code given by the poster.
26th Mar 2020, 5:13 PM
HonFu
HonFu - avatar
+ 4
loop, I don't really know a lot about web. I'd probably need five times as long as you to find it. 😅
26th Mar 2020, 6:12 PM
HonFu
HonFu - avatar
+ 3
it's something like that: a, b, s=0,1,0 while b<4e6: a, b = b, a+b if not b%2: s+=b print(s)
26th Mar 2020, 4:31 PM
John Robotane
John Robotane - avatar
+ 2
Natalie, please do not use the q&a forum to chat or link stuff you find interesting. This place is reserved for programming- and Sololearn-related questions. You can talk more freely in activity feed or direct message. https://www.sololearn.com/discuss/1316935/?ref=app
28th Mar 2020, 9:47 AM
HonFu
HonFu - avatar
+ 1
fibonacci1st ,fibonacci2nd = 0,1 sum_even_valued_terms = 0 while fibonacci2nd < 4e6: fibonacci1st ,fibonacci2nd = fibonacci2nd ,fibonacci1st + fibonacci2nd if fibonacci2nd % 2 ==0: sum_even_valued_terms += fibonacci2nd print(sum_even_valued_terms
26th Mar 2020, 4:28 PM
John Robotane
John Robotane - avatar
+ 1
Thanks John Robotana🙂. It will help me a lot ......
26th Mar 2020, 4:29 PM
Md. Mursalatul Islam Pallob
Md. Mursalatul Islam Pallob - avatar
+ 1
Thanks HonFu for your valuable advice ......🙂🙂🙂🙂🙂 Next time i will remember what you said .
26th Mar 2020, 4:39 PM
Md. Mursalatul Islam Pallob
Md. Mursalatul Islam Pallob - avatar
+ 1
a=0 b=1 s=0 while a <= 4e6: If not a%2: s+=a a,b=a+b,a
28th Mar 2020, 6:16 AM
Akbar Ali Quazi
Akbar Ali Quazi - avatar
+ 1
Bro its seems like you are genius because I'm not getting it 🙄🙄🙄
28th Mar 2020, 6:46 AM
Xyz
Xyz - avatar
28th Mar 2020, 10:46 AM
Syed Ahmed Raza Shah
Syed Ahmed Raza Shah - avatar
+ 1
fibo1, fibo2 = 0, 1 sum = 0 while fibo2 < 4e6: fibo3 = fibo1 + fibo2 if fibo3 >4e6: break fibo1, fibo2 = fibo2, fibo3 if fibo2 % 2 ==0: sum += fibo2 print(sum)
28th Mar 2020, 3:50 PM
SIDDIQUE ZEESHAN AZAM
SIDDIQUE ZEESHAN AZAM - avatar
0
26th Mar 2020, 4:32 PM
John Robotane
John Robotane - avatar
0
You have any suggestion for me John Robotane for do more effective coding ?🙂
26th Mar 2020, 4:34 PM
Md. Mursalatul Islam Pallob
Md. Mursalatul Islam Pallob - avatar
0
Hello
26th Mar 2020, 10:41 PM
Василий Моисеев
Василий Моисеев - avatar
0
Hii🙂☺
27th Mar 2020, 12:56 AM
Md. Mursalatul Islam Pallob
Md. Mursalatul Islam Pallob - avatar
- 1
Может кто-нибудь помочь с задачей? "3. Программа получает на вход последовательность из N целых неотрицательных чисел. Найти количество всех цифр в последовательности чисел. Ввод данных: N = 5 23 475 12 1 683" Нужно решить на Питоне и с использованием цикла while. Понятие того, что можно сделать, есть, но в голову не приходит, как это написать. Буду благодарить вас за помощь столько, сколько хотите, но помощь сильно нужна. Спасибо заранее!
27th Mar 2020, 11:05 AM
Олень
Олень - avatar