Reputation: 13
print "This program computes and prints the sum of all even values"
print "between 2 and a positive integer value entered by the user. \n"
integer = input("Enter a positive integer: ")
while integer <2:
if integer <2:
print "Integer must be greater than 2!"
integer = input("Enter a positive integer: ")
else:
integer2 = input("Now enter a second integer: ")
evens = (integer - integer2)/2 + 1
while True
I have to be able to ask the user for two numbers, and then my program should be able to add up all of the even numbers between the two numbers. I just started programming, so I haven't learned much. I tried looking around for answers, but the answers here didn't make any sense to me because they were using techniques that were too advanced for me. Thanks!
Upvotes: 0
Views: 12206
Reputation: 11
def addEvenNumbers(num, num1):
total=0
for v in range(num,num1+1):
if v>=0 and v % 2 == 0:
total+=v
return total
Upvotes: 0
Reputation: 495
First, your opening print statements mis-state what the program is supposed to do.
Second, be very specific. Do you wish to include the endpoints in the sum if they are even? The problem statement, as given, does NOT include them.
Third, use your head before you program anything. The sum of all even integers, starting from 2, and ending at 2N is N(N+1). If you start instead from another even number, 2L, where 2L>2, then you merely have to subtract out the part from 2 to 2L-2. But you know what that is: it's (L-1)L, where all I did was substitute L-1 for N in the "sum of all even integers starting from 2" equation. So, the sum of all even integers, starting from 2L and ending at 2N is
N(N+1)-(L-1)L, where 2L and 2N are included in the sum.
Fourth, set up code to make sure you are using the correct start/end points. Keep in mind that the user might use 2 evens, 2 odds, odd-even, or even-odd. You have to account for all cases.
Fifth, while you are testing any program, print out as many intermediate values as you can. That will make any bugs obvious. You can comment out those print statements later.
Sixth, test your program on easy cases.
Upvotes: 0
Reputation: 41
Here's a quick example using the interactive shell:
>>> x = 9
>>> y = 31
>>> sum([z for z in range(x, y + 1) if z % 2 == 0])
220
This uses something called list comprehension and the built-in method sum.
Now, for an explanation on how this all works together:
Range, as you already know, returns a list of numbers between its two arguments.
>>> range(x, y + 1)
[9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31]
The modulo operator (%) is used to divide two numbers and then give you the remainder. This makes it very handy to find even numbers: any even number divided by 2 will have a remainder of 0.
>>> 5 % 2
1
>>> 4 % 2
0
The list comprehension uses this trick to build a list of values containing every even number in the given range.
>>> [z for z in range(x, y + 1) if z % 2 == 0]
[10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30]
Finally, sum()
iterates and adds up all the values generated by that list for you.
>>> sum([z for z in range(x, y + 1) if z % 2 == 0])
220
Upvotes: 1
Reputation: 1586
All the answers given address the problem, but in a different way than you are trying to (IE, using an equation instead of either language features or a basic loop... which will degrade in performance if passing in a very large range), so let me try and help you with your equation.
There are a couple things going wrong. Probably the most fundamental issue is the logic behind your expression.
For example, what do you expect the output of the following inputs to be vs what your expression should output...
Assuming absolute value and floor rounding...
(5, 7), expect 1, (5 - 7) / 2 + 1 = 2
(6, 8), expect 0, (6 - 8) / 2 + 1 = 2
(6, 7), expect 0, (6 - 7) / 2 + 1 = 1
It seems that you need to detect if one of the integers is in fact even... integer % 2 == 0
so for an exclusive range...
(6, 8), expect 0, (6 - 8) / 2 = 1, 6 is even so subtract 1 => 0
(5, 7), expect 1, (5 - 7) / 2 = 1, 5 is odd so do nothing => 1
(6, 7), expect 0, (6 - 7) / 2 = 0.5 (round up to 1), 6 is even, so subtract 1 => 0
(7, 8), expect 0, (7 - 8) / 2 = 0.5 (round up to 1), 8 is even, so subtract 1 => 0
(7, 10), expect 1, (7 - 10) / 2 = 1.5 (round up to 2), 10 is even, so subtract 1 => 1
Now, I think this should work for all input values. Lets modify the code.
import math
....
evens = math.ceil(math.fabs((integer - integer2) / 2.0))
if integer % 2 == 0 or integer2 % 2 == 0:
evens -= 1
And if you want to make it for an inclusive range, IE (6, 8) returns 2...
evensInclusive = evens + (integer + 1) % 2 + (integer2 + 1) % 2
Upvotes: 0
Reputation: 82450
This should do the trick:
start = 0
end = 0
while start < 2 or end <= start:
start = int(raw_input("Enter start -> "))
end = int(raw_input("Enter end ->"))
total = 0
for x in range(start, end+1):
if x % 2 == 0:
total += x
print total
You can make it even more succinct using list comprehensions:
start = 0
end = 0
while start < 2 or end <= start:
start = int(raw_input("Enter start -> "))
end = int(raw_input("Enter end -> "))
print sum([x for x in range(start, end+1) if x % 2 == 0])
Not that for both range functions, I have used end+1
because range will only go up to the number before the second parameter to the function.
Upvotes: 0
Reputation: 20173
In order to generate a list of integers you can use the range function.
range(10, 20)
>> [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
Given a list of integers you can filter it by using a list comprehension.
[number for number in [1, 2, 3, 4, 5, 6] if not number % 2]
>> [10, 12, 14, 16, 18]
Given a list of integers, you can calculate the sum by using the built-in sum
function.
sum([1, 2, 3])
>> 6
You can combine all this and obtain your answer in a simple statement.
sum([even for even in range(10, 20) if not even % 2])
>> 70
Upvotes: 0
Reputation: 3913
Once you have the two numbers you need to find all even ones between them, right?
Well, to find all the numbers between them you use range(integer,integer2+1)
(the +1
is there because I'm assuming you want it including the integer2
).
To find all even numbers in that range you use filter(lambda x: x%2==0, range(integer, integer2+1))
and then to sum it all you use sum()
.. so in the end:
sum(filter(lambda x: x%2==0, range(integer, integer2+1)))
Upvotes: 0