Python Forum
python homework print the sum of a range of numbers from x to y
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python homework print the sum of a range of numbers from x to y
#1
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.")
Reply
#2
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.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
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)))
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#4
Thank you wavic and DeaD_EyE. I will work on your recommendations.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Random Generator: From Word to Numbers, from Numbers to n possibles Words Yamiyozx 2 1,407 Jan-02-2023, 05:08 PM
Last Post: deanhystad
  HELP in python homework makashito 4 3,902 Oct-12-2021, 10:12 AM
Last Post: buran
  CyperSecurity Using Python HomeWork ward1995 1 1,949 Jul-08-2021, 03:55 PM
Last Post: buran
Exclamation urgent , Python homework alm 2 2,288 May-09-2021, 11:19 AM
Last Post: Yoriz
  Convert list of numbers to string of numbers kam_uk 5 2,989 Nov-21-2020, 03:10 PM
Last Post: deanhystad
  Homework with python Johnsonmfw 1 1,676 Sep-20-2020, 04:03 AM
Last Post: ndc85430
  Python Homework Help *Urgent GS31 2 2,570 Nov-24-2019, 01:41 PM
Last Post: ichabod801
  Scaling random numbers within specific range schniefen 4 3,169 Oct-28-2019, 11:22 AM
Last Post: jefsummers
  How to Generate and Print An Array with Random Numbers in Python in the johnnynitro99293 14 9,542 Sep-20-2019, 11:56 AM
Last Post: ichabod801
  Python Homework Question OrcDroid123 1 2,364 Sep-01-2019, 08:44 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020