#WSQ07

This time we had to create a program that shows the result of a inclusive sum of numbers in a random range that the user has to pick.

 

-First we have to make sure that the user types the lower and higher number the range correctly, that means tha the lower number has to be lesser than the last number of the range.

x=0
y=0
print (“This program will calculate the sum of integers in the range you provide”)
while (int (x) >= int (y)):
x=int( input(“Please give me the lower bound of the range:”))
y= int (input (“Please give me the upper bound of the range:”))

if (x>=y):
print (“The lower bound has to be smaller than the upper bound”)
print(“”)

 

-Then we have to make our code sum all of the number between that range, in order to do so we have to use a for loop. 

num = 0
for n in range (x, y +1):
num= num + n

 

 

 

So the final code looks like this: 

sumoftl

 

And this is the program running. 

sum

CC BY-SA 4.0 #WSQ07 by alextenablog is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.