Question: In Python: Write a few lines of code that will add the numbers from 1 to 12 inclusive and print the final sum . sum
In Python:
Write a few lines of code that will add the numbers from 1 to 12 inclusive and print the final sum.
sum = 0
for j in range ( ___, ____):
sum = sum + j
print(sum)
Output:
B. Write a few lines of code that will add the squares of the numbers from 1 to 12 inclusive and print the final sum. (Check that your first value added is 12 and that your last value added is 122.) Use range, not a list.
sum = 0
for j in range (___ , ___):
sum = sum + j
print(sum)
1) What is the output?
2) Is it necessary to initialize the sum to 0 first? Try leaving it out and see what happens. Report what happens here:
3) What happens if you initialize the sum to 0 inside the loop? Try it and report what happens here:
4) What happens if you print the sum inside the loop?
What happens if you print the sum outside (after) the loop?
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
