Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to decide with a School task on Python

I can’t solve a child’s problem in any way, it seems to be simple, but I can’t think of a solution. Here is my resulting code.
P.s
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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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, please help.

>Solution :

It is a very simple question, This are basics of programing (with python) and it’s not a good question for here (Stackoverflow), this is why you’re getting down votes!

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):
  ...
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading