Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Inference Control In Statistical Databases From Theory To Practice Lncs 2316

Authors: Josep Domingo-Ferrer

2002nd Edition

3540436146, 978-3540436140

More Books

Students also viewed these Databases questions

Question

Compare and contrast an NAP and a MAE.

Answered: 1 week ago