How to write a function

Preformatted text loading_screen():
print(“This page is loading…”)
#call loading_screen
loading_screen()

IT won’t call the loading screen why?

looked at the solution it’s the same way where’s the error?

Hi, it’s unclear with your formatting at the moment. Indentation is vital for it to work.

def loading_screen():
  print("This page is loading…")

#call loading_screen
loading_screen()
1 Like

I indented the function i ended it now i have to call the loading screen i fixed the indent it it just says Expected to see ‘’ This page is loading…" printed so where did i go wrong?

Is it because i never ended the function?

From your first post, it looks like you didn’t put def when defining the function. You need to define a function properly before you can call it.

1 Like

i put def in the beginning now when i call loading_screen outside the function it’s still indented and i get an error how can I fix this


this what it comes up as

It’s the indentation.

The way you have it right now. The compiler is going to interpret loading_screen() being called recursively within itself (by recursive, I mean a function that calls itself within its definition, which is sometimes desirable but is more advanced). You want to make sure you don’t indent the invocation if you want to avoid recursion.

Notice how in the previous example I gave you, the loading_screen() is not indented or double-spaced.

Correct:

def loading_screen():
  print("This page is loading…")

#call loading_screen
loading_screen()

Recursive, and in this context, not correct:

def loading_screen():
  print("This page is loading…")
  #call loading_screen recursively
  loading_screen()
  # any text indented or spaced <--- like this is going to 
  # be part of the scope of the definition of the function

# <--- but if it's here it will be outside the scope of the definition
2 Likes

just tried the double space but it still ends up being part of the indent how to I end the indent

There should be no indent or spaces for this line. Maybe go over once again @toastedpitabread’s explanation above which does a great job of explaining why there should be no indentation.

iI get what he’s saying but when I go to move it out of the function it still stays in the function

So my question is how do i get it to reset to it to being completely out of the function as it is in the first line.

Never mind figured it out

2 Likes

I’m having the same issue, would you be able to help me out?

Welcome to the forums!

See this reply and remember that Python relies on indentation, unlike some other languages which make use of curly braces or other methods of indicating what is within which scope.