Python while loop
BartC
bc at freeuk.com
Tue Nov 29 20:03:05 EST 2016
On 29/11/2016 23:58, paul.garcia2345 at gmail.com wrote:
> Write a program which prints the sum of numbers from 1 to 101 ( 1 and 101 are included) that are divisible by 5 (Use while loop)
>
> This is the code:
>
> x=0
> count=0
> while x<=100:
> if x%5==0:
> count=count+x
> x=x+1
> print(count)
This looks at numbers from 0 to 100 inclusive, not 1 to 101. Although it
doesn't affect the result. (It will add in 0 which is divisible by 5,
but that doesn't change it either. If this is an exercise however,
checking the correct range might be important!)
--
Bartc
More information about the Python-list
mailing list