[Tutor] Outstanding Python Issue

David Rock david at graniteweb.com
Tue Aug 11 20:06:37 EDT 2020


> On Aug 11, 2020, at 14:27, eric grunfeld <grunfelderic at gmail.com> wrote:
> 
> Hello Everyone:
> 
> I have been having some issues with this function in Python:
> 
> Here it is:
> 
> def convert_distance(miles):
>    km = miles * 1.6  # approximately 1.6 km in 1 mile
> return km
> 
> my_trip_miles = 55
> 
> This has been the response:
> 
> Error on line 4: return km ^ SyntaxError: 'return' outside function
> Because I am new to Python programming, I am sure that there is an easy
> solution.

Hi, Eric.

It’s because Python uses leading whitespace indentation to tell where code blocks begin and end.  Your return statement is outdented, so it’s not part of the function.  You need to indent it so it’s the same depth as the km = miles * 1.6 line so it’s “inside” the function.

— 
David Rock
david at graniteweb.com






More information about the Tutor mailing list