n = 7
acc = 0
for i in range(1, n + 1):
if i % 2 == 0:
acc += i
print(acc)
This is a solution for approach B
:
n = 7
sum_even = 0
sum_odd = 0
for i in range(1, n + 1):
if i % 2 == 0:
sum_even += i
else:
sum_odd += i
print("sum of even numbers is", sum_even)
print("sum of odd numbers is", sum_odd)
If we write print(..., end=" ")
we do not separate the output of following print
statements with a new line, but with the given :
for i in range(1, 31):
if i % 15 == 0:
print("fizz buzz", end=" ")
elif i % 3 == 0:
print("fizz", end=" ")
elif i % 5 == 0:
print("buzz", end=" ")
else:
print(i, end=" ")
Hint: replace the end
parameter with aribraty strings and see how the output changes !
for i in range(1500, 2001):
if i % 7 == 0 and i % 13 == 0:
print(i)
Read nip
as n_is_prime
(n_is_prime
is the better name, but I did not make it to obvious what the program computes).
The program checks for a given $n$ if there is an i in range $2 \le i \lt n$ which is a divisor of $n$. Eventually it prints True
if such a divisor was not found and False
else. So it checks if $n$ is a prime number !
for n in range(2, 20):
nip = True
for i in range(2, n):
if n % i == 0:
nip = False
if nip:
print(n)