Is there an error?

The following Python code is meant to output the first 10 positive perfect squares .

What will happen when the code is executed?

1
2
for i in range(10):
    print(i**2)


Note: You are not allowed to run the code using a Python interpreter!

Note: The syntax highlighting is off on purpose!

Runtime error Syntax error Semantic error Code works as planned

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

1 solution

Nick Schreiner
Feb 14, 2017

The variable i will loop from 0 to 9. Since the desired output is the first 10 positive squares, and we are including 0, the output will not be what we intend. This logical issue is called a semantic error.

In contrast, a syntax error is an issue with the language (for example, forgetting the : after the for statement) and is typically caught by your IDE before the program even begins. A runtime error is any uncaught error that happens at runtime. A common example might be a divide by zero error.

Upvote for the additional explanation of syntax and runtime errors!

Christopher Boo - 4 years, 3 months ago

I'm not yet familiar with Python; I guessed it was a syntax error because print didn't end in a semicolon which would be a problem in C++.

Guy Alves - 4 years, 3 months ago

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...