The new forums will be named Coin Return (based on the most recent
vote)! You can check on the status and timeline of the transition to the new forums
here.
The Guiding Principles and New Rules
document is now in effect.
Python question (class help)
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!
0
Posts
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'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.
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.)
Steam Profile | Signature art by Alexandra 'Lexxy' Douglass
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.
Steam Profile | Signature art by Alexandra 'Lexxy' Douglass
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".