Mathematically, there’s a really cool operation we could use to get the integer sum of [n_1, n_2], so when I read that that wasn’t what we had to do (at least not exactly) it felt more interesting.
Here’s the GitHub code link:
]]>This is my code:
print("I will calculate the sum of numbers in a range you select.")
numLow = numLow1 = int(input("What is the lower bound? "))
numUp = int(input("What is the upper bound? "))
x = int(numLow)
if numLow <= numUp :
while numLow != numUp :
numLow = numLow + 1
x = x + numLow
print("The sum of numbers in the range between", numLow1,"and",numUp,"is",x)
else:
print("You didn't type in the numbers in the right order.")
print(numCom,"was the right number!")
print("You needed",attempt,"attempts.")
]]>In this occasion Ken was very helpful because he write in the blackboard how to do it
Here is the link to my GitHub
https://github.com/ReneOro97/-WSQ/blob/master/%23WSQ07
In this occasion Ken was very helpful because he write in the blackboard how to do it
Here is the link to my GitHub
https://github.com/ReneOro97/-WSQ/blob/master/%23WSQ07
The task is meant for us to learn to use loops, and i selected to use the For Loop. This is so far one of the hardest parts of programing i’ve learned and according to what i´ve read is one of the most powerful.
A for loop consts of various parts, i understand it like this:
for (variable we´ll use for repeating the process) in (under what conditions it will repeat):
what to do once we’re inside the loop
The code i wrote is:
print(“Hey! This programm will ask you for two integers, the beginning”)
print(“and the end of a serie of numbers”)
print(“So the program will display the addition of all the nubers”)
print(“within the range of the ones you provide us”)
print()
x = int(input(‘Please type the first number of your serie: ‘))
print()
y = int(input(‘Please type the second number of the serie: ‘))
c = 0
print()
for i in range(x, (y+1)):
c = c + i
print(‘The sum of ‘,x, ‘ to ‘, y, ‘ (inclusive) is: ‘, c)
I was thinking of ther solutions but once i figured how to do it with for, i realize it only takes me two lines.
So, the explaining of my code is that first i asked the numbers to the user. Then i had to set the first number that i had to add, i choose 0 because it wont affect my operation. Then wrote the for loop. I started with ‘for’ and the i followed with the variable that will repeat the process of the loop, i selected the letter ‘i’. Then i type ‘in range (x,(y+1))’ which means that the loop must repeat in the range of the numbers the user gave me. If you are wondering why i set ‘y+1’ thats because the function range is composed to start where it is told (or zero if it doesn´t say where to start) and to end where it is told minus one. So to fix this i simple add it one.
The the body of the loop is that c = c+i. Lets remember that C starts being zero, so the first will be ‘0 + i’ where ‘i’ will start being the first number the user gave me. So after that the loop will start all over again, now ‘i’ will be ‘i+1’ because of the body and ‘c’ again will be the previous c we saved on memory plus the actual value of i. This will be performed until ‘i’ reaches the second value the user gave us. Then the sum of all number, inclusive, will be done and ready to be printed.
]]>This is my code:
This is Sumofnumbers.py in action:
]]>