Python Forum
Question about my code - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Question about my code (/thread-27470.html)



Question about my code - Than999 - Jun-07-2020

Hi, I am struggling to understand the solution to my homework problem and why my code or logic is incorrect compare to the solution of this question. The assignment states "write a for loop that sums the values 1 through end, inclusive. end is a variable that we define for you. So, for example, if we define end to be 6, your code should print out the result:

21

which is 1 + 2 + 3 + 4 + 5 + 6.

For problems such as these, do not include input statements or define variables we will provide for you. Our automating testing will provide values so write your code in the following box assuming these variables are already defined."

Now here is my code:

SumNum = 0
for num in range(0, end):
    SumNum += num
    print(SumNum)
Now, I am not sure why my code is wrong. It's from my understanding that when num iterates through the for loop from 0 to end, the value of each iteration is added to SumNum, and then gets printed out. I thought this is what the assignment is asking for.

But here is the actual solution to this assignment:
SumNum = 0
for num in range(1, end+1):
    SumNum += num
print(SumNum)
What I don't understand in this solution is why the range is starting from 1 instead of 0? Why is the print function outside of the for loop instead of inside the for loop?

I think my code is very similar to the answer, the main difference is that I start from 0 instead of 1. So what is the issue? Any explanation would be greatly appreciated.


RE: Question about my code - pyzyx3qwerty - Jun-08-2020

1. in your code, it will print all numbers till end, excluding end and in your answer, it will print end also
2. keeping print statement outside for loop results in the whole thing printed in 1 time, but in the for loop
it will create rows till you reach your number


RE: Question about my code - snippsat - Jun-08-2020

(Jun-07-2020, 11:42 PM)Than999 Wrote: What I don't understand in this solution is why the range is starting from 1 instead of 0?
It make no difference when summing it up,can look at range() to see what's going on.
>>> list(range(6))
[0, 1, 2, 3, 4, 5]
>>> list(range(0, 6))
[0, 1, 2, 3, 4, 5]
>>> list(range(1, 6))
[1, 2, 3, 4, 5] 
So Python(as most programming langues) start to count from 0.
When summing these up can use build sum() as you have written as a exercise,
then it will not be 21 because start a 0.
>>> sum(range(6))
15
>>> sum(range(0 ,6))
15
>>> sum(range(1, 6))
15
>>> 
>>> # So most add 1 to get 21,so starting a 0 or 1 make difference when use sum() or your code
>>> sum(range(7))
21
>>> sum(range(0 ,7))
21
>>> sum(range(1 ,7))
21



RE: Question about my code - buran - Jun-08-2020

(Jun-07-2020, 11:42 PM)Than999 Wrote: why the range is starting from 1 instead of 0?
They start range at 1 because the assignment specifically ask you to start at 1 - "the values 1 through end". Yes, adding 0 will not affect the final result/sum, but technically it's not in accordance with the assignment.

(Jun-07-2020, 11:42 PM)Than999 Wrote: Why is the print function outside of the for loop instead of inside the for loop?
Again, the assignment ask you to print the [final] result. They don't ask you to print intermediate results.

Then, there is end+1 difference - you don't ask about it, but it's the main issue in your solution. As already explained - range(start:stop:step) does not include stop. So, you need to use range(1, end+1) in order to get end when iterate over the range object


RE: Question about my code - Emekadavid - Jun-08-2020

In fact you have to understand range as a reference to the number line.
If you try:
 
dlist = list(range(-1, 3)
print(dlist)
The output will show you that range is just moving along the number line. It starts with -1 and goes through to 2.


RE: Question about my code - jefsummers - Jun-08-2020

range() never includes the end number. That's just something you have to learn in Python or it will byte you. That is actually likely the main point of the lesson.