A
A
Artem Marenkov2020-08-15 16:44:52
Python
Artem Marenkov, 2020-08-15 16:44:52

Why doesn't the code work with inputs of 7?

Friends, hello everyone! I decided to solve olympiads for grades 9-11 in computer science. Python was chosen as the language. My skills did not last long. The problem is this:

Sid solved this problem "Problem 2. Even - odd Masha loves even numbers, and Misha loves odd numbers. Therefore, they are always happy if they meet numbers that they like.

Today they met all the integers from A to B inclusive Masha decided to calculate the sum of all even numbers from A to B, and Misha - the sum of all odd ones, after which they started arguing who got the sum more.Help them - find the difference between Masha's sum and Misha's sum.

The program receives as input two positive integers A and B, not exceeding 2×109. The program should output one number - the difference between the sum of even numbers and the sum of odd numbers from A to B. Examples of input and output data Input 3 6 Output 2 Note The sum of even numbers is 4 + 6 = 10, the sum of odd numbers is 3 + 5 = 8 , the difference is 2. Input 3 7 Output -5 Note The sum of even numbers is 4 + 6 = 10, the sum of odd numbers is 3 + 5 = 8, the difference is 2

" everything is ok, it turns out and everything works, and when 3 and 7, then the console is just silent and that's it.Maybe someone knows what the problem is?Here is the code:

num_1 = int(input())
num_2 = int(input())
i = num_1

chet_sum = 0
nechet_sum = 0

# пока i больше или равно num_1 и пока i меньше или равно num_2
while i>=num_1 and i<=num_2:

    # если при делении по модулю на i на 2 остаток равен нулю 
    if i % 2 == 0:
        chet_sum = chet_sum + i
        i = i+1
        continue

    # если при делении по модулю на i на 2 остаток равен не нулю 
    if i % 2 != 0 and i != num_2:
        nechet_sum = nechet_sum + i
        i = i+1
        continue

print(chet_sum - nechet_sum)

input()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GavriKos, 2020-08-15
@Mart14

if i % 2 != 0 and i != num_2:
Well, what branch should the code follow when i==7, i.e. at the end of the sweep?
And by the way, IMHO, this task should not be solved by exhaustive enumeration - the tests simply will not pass in time.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question