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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started