How to decide with a School task on Python

Question:

Here is my resulting code.

The task says to try to solve it through while.

The task
A positive integer from the segment [3;50] is entered from the keyboard.

Find the sum of all positive even numbers strictly less than the given one.

n = int(input(f'Введите чило № '))
k = 0
while n>3 and n<50:
  for e in range(3<n<50):
  k = n + e
  print(k)

The most difficult thing for me is how to make the code consider what is less than the entered number.
I thought for a couple of hours.

Asked By: Moksggwp

||

Answers:

if you have to use while this is the answer.

n = int(input(f'Введите чило № '))
n -= n%2
s = 0
while n>0:
  s += n
  n -= 2

or

n = int(input(f'Введите чило № '))
i = 2
s = 0
while i<n:
  s += i
  i += 2

But the best way is:

n = input(...)
s = sum(range(0,n,2))

And after all you can not write range(3<n<50), it does not mean anything in python, the syntax is range(start,end,step) for example you should write as follow to go trough 3 to 50:

for i in range(3,50):
  ...

or use step to just get even (or odd) numbers in for loop:

for i in range(0,50,2):
  ...
Answered By: BSimjoo
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.