+ 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
22 Answers
+ 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.
+ 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.
+ 4
loop, I don't really know a lot about web. I'd probably need five times as long as you to find it. 😅
+ 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)
+ 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
+ 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
+ 1
Thanks John Robotana🙂. It will help me a lot ......
+ 1
Thanks HonFu for your valuable advice ......🙂🙂🙂🙂🙂
Next time i will remember what you said .
+ 1
a=0
b=1
s=0
while a <= 4e6:
If not a%2: s+=a
a,b=a+b,a
+ 1
Bro its seems like you are genius because I'm not getting it 🙄🙄🙄
+ 1
Sololearn developers survey
https://code.sololearn.com/WApwLILsnNdR/?ref=app
+ 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)
0
thanks Mursalatul Pallob ...
0
You have any suggestion for me John Robotane for do more effective coding ?🙂
0
Hello
0
Hii🙂☺
- 1
Может кто-нибудь помочь с задачей?
"3. Программа получает на вход последовательность из N целых неотрицательных чисел. Найти количество всех цифр в последовательности чисел.
Ввод данных:
N = 5
23 475 12 1 683"
Нужно решить на Питоне и с использованием цикла while. Понятие того, что можно сделать, есть, но в голову не приходит, как это написать. Буду благодарить вас за помощь столько, сколько хотите, но помощь сильно нужна. Спасибо заранее!