Python Forum
python homework print the sum of a range of numbers from x to y - 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: python homework print the sum of a range of numbers from x to y (/thread-13706.html)



python homework print the sum of a range of numbers from x to y - kirito85 - Oct-28-2018

Hi, my for loop is not working, i can't figure out how to write the coding for this sum of range of x to y. Thanks.

 
import statistics 
print("This program prints the sum of a range of numbers from x to y")
print("For example, if x is 10 and y is 50, the program will print the sum of numbers from 10 to 50")
x = str(input('Please enter the value of x:' ))
y = str(input('Please enter the value of y:' ))


if x.isnumeric() and y.isnumeric():
    if (int(x) > 0) and (int(y) > 0):
        if int(y) > int(x): 
            sum_of_numbers = 0 
            counter = 0
            for counter in range(int(x), int(y)):
                sum_of_numbers += 1
                print("The sum of numbers between", x, "and", y, "is", sum_of_numbers)
            
        else:
            print("You did not enter a value of y that is greater than x!")
            print("Unable to continue. Program terminated.")        
    else:
        print("One or more of your inputs are not greater than zero")
        print("Unable to continue. Program terminated.")
else:
        print("One or more of your inputs are not numeric!")
        print("Unable to continue. Program terminated.")



RE: python homework print the sum of a range of numbers from x to y - wavic - Oct-28-2018

print("The sum of numbers between", x, "and", y, "is", sum_of_numbers)
must be out of the loop.
Also, why are you adding 1 to the sum and not the counter? I think naming it 'counter' confused you to do this.

Print the counter to see what it is on every loop.

The input is always a string. You don't have explicitly make it one.


RE: python homework print the sum of a range of numbers from x to y - DeaD_EyE - Oct-28-2018

line number 12: you assign 0 to the variable counter. You don't need to pre-assign a variable, which is used in a for loop.
line number 13: the for loop assign to counter. You should rename this variable.
line number 14: you adding one to the sum_of_numbers. Do you want to calculate 1+2+3+4+5.. or just the number of values?
line number 15: dedent this line and you get the result after the loop is done. Otherwise you get a result for each iteration.

In line 9, 10 and 13 you are converting the input over and over to an int. One time is enough.
You can reassign the integer to the variable and reuse it.

In line 4 and 5 you're converting the input to a str. The input function always returns a str.

Line 1: statistis is currently not used in this snippet. Don't import it, when you don't using it. Linters will complain about this.

The code is nested to deep. You should split it up as functions.
One function for input x and y and the convert to int, and one function to do the logic.
Then each function does only one thing. Handling input data together with calculations makes the code to complex.

The usage can look like this:
def input_range():
    pass

def make_sum(x, y):
    pass

x, y = input_range()
make_sum(x, y)
By the way, if you only want to know the len of a list, tuple or other container objects, use len.
The range function is very special and supports also the len function.
print(len(range(1, 11)))


RE: python homework print the sum of a range of numbers from x to y - kirito85 - Oct-28-2018

Thank you wavic and DeaD_EyE. I will work on your recommendations.