0

Can i use for loop instead while loop

This code i make is reverse of the sum of consecutive number .so it can output the range .e.g input = 10 ,10 = 1+2+3+4 , output is 4. https://code.sololearn.com/c54u7h3Mj4P0/?ref=app

20th May 2022, 1:52 AM
Aisy Danish Mohd Nazry
Aisy Danish Mohd Nazry - avatar
3 ответов
+ 1
Aisy Danish Mohd Nazry , i did also a try that matches the output of your code: from itertools import cycle y = int(input()) sum = 0 for _ in cycle([None]): # this generates an infinite for loop by using an iterator given by cycle() if y <= 0: break sum += 1 y -= sum print(sum) some comments: >>> we should not use the name 'sum' as variable name, since this is overwriting the built-in function sum() Prince Kumar , if you may like to get the same result out of your code compared to the code posted from the op it should be revised - missing output for some input numbers - different output for some input numers [EDITED Prince Kumar ] comparing the results of the original code and your attempt: missing output for input value: 2,4,5,7,8,9,11 correct for 1,3,6,10
20th May 2022, 10:38 AM
Lothar
Lothar - avatar