# Python question (class help)

The fun has been DOUBLED!Registered User regular
So, I'm doing an exercise for my intro to Python class which wants me to find the sum of all even numbers between 5 and 55. This is easy enough to do by doing the following:

total_sum = 0
for num in range(6, 55, 2)
total_sum = total_sum + num

print(total_sum)

Yet for some reason the thing demands I use 5 as the lower bound number which gives a different answer than it wants. I've searched my guide and the internet but so far I haven't found a solution. Given this is week 3 in an intro class, I can't imagine it's expecting a ton of added code. I don't know why it won't just let me use what I wrote, but such is life.

Any help would be appreciated!

## Posts

• PICNIC BADASS Registered User, ClubPA regular
edited April 2023
The index (2) in your solution just counts every second number in your range from the starting point of your list (6->55), meaning you're not actually checking whether the number is even. That's why the answer is different when you put in 5 as a lower bound - then it's counting 5,7,9 ... etc.

You're relying on the user to input the information in a particular way, which isn't ideal practice. Think about what it means for a number to be even - yes, usually that means it's a number that comes after an odd number, but what features does it have that allow you to test even-ness? (additionally the way the question is worded has several indicators (eg. using the word 'between') which hint at the form of the solution that they want - they specifically call out [5,55) as the range, so that should be your input).

My guess is that they want you to write a function that you can plug any two numbers into and find the sum of all even numbers in between regardless of starting point, eg
find_even_sum(5,55)
find_even_sum(4,71)
... etc
This function call should access the same code chunk and output the correct answer every time.

tynic on
• Tandem Electrostatic Accelerator Registered User regular
In most languages, a very specific function is implemented to to find remainders from division. You can use this to sort even and odd integers quite easily.

I've spoilered the name of the function, in case you want to try and find it yourself. But don't feel bad if you do look because Python is a vast language with often multiple ways to do the same thing and finding what you want can be difficult especially when starting out.
The function is:
Modulo operator/function

Steam - Synthetic Violence | XBOX Live - Cannonfuse | PSN - CastleBravo | Twitch - SoggybiscuitPA
• The fun has been DOUBLED! Registered User regular
tynic wrote: »
The index (2) in your solution just counts every second number in your range from the starting point of your list (6->55), meaning you're not actually checking whether the number is even. That's why the answer is different when you put in 5 as a lower bound - then it's counting 5,7,9 ... etc.

You're relying on the user to input the information in a particular way, which isn't ideal practice. Think about what it means for a number to be even - yes, usually that means it's a number that comes after an odd number, but what features does it have that allow you to test even-ness? (additionally the way the question is worded has several indicators (eg. using the word 'between') which hint at the form of the solution that they want - they specifically call out [5,55) as the range, so that should be your input).

My guess is that they want you to write a function that you can plug any two numbers into and find the sum of all even numbers in between regardless of starting point, eg
find_even_sum(5,55)
find_even_sum(4,71)
... etc
This function call should access the same code chunk and output the correct answer every time.

I get that, but none of that is explained in the literature (so far!) and it just seems weird to me that they would expect you to go out of your way to use something that hasn't been discussed.

So far it's the only one that doesn't reference something in the literature or something that would logically follow from what's written.

I'll give it a go and see what it says. Currently I'm working on one that wants me to set a condition to check for a type (among other things) but it doesn't like me using the only command it's provided so far.

(Online classes really suck sometimes, I swear.)

• PICNIC BADASS Registered User, ClubPA regular
Even if you don't solve it using modulo and instead stick with a range indexed solution, its gonna demand you use 5 as a lower input cause that's how the question is framed - computers are rigid like that.

It's possible they're expecting you to read ahead, or they've just got a very brittle parser - the latter is definitely one of the worst components of online learning.

• The fun has been DOUBLED! Registered User regular
Yeah, a lot of my instructors are very slow on responses (if they respond at all). At least it's my last semester so I can start working on other things.

• Registered User regular
Not using modulo I think you can do:
total_sum = 0
for num in range(5, 55, 2):
if num < 55:
total_sum = total_sum + (num + 1)


So if we run a couple steps:

total_sum = 0
num = 5

total_sum = 0 + (5 + 1)

->

total_sum = 6
num = 7

total_sum = 6 + (7 + 1)

->

total_sum = 14
num = 9

total_sum = 14 + (9 + 1)

And so forth, stopping at num = 53, because if we hit num = 55 then we'll wind up adding 56 to the total which is wrong.

It seems a bit silly, but I think this might be what the parser is looking for? Early problems are always weird because the real-world solution is often just "Call library 'do_this_sort_of_thing' on the input".

Take a moment to donate what you can to Critical Resistance and Black Lives Matter.