Help: how to fix problem with SyntaxError: 'return' outside function

How to fix this? This is my code:

# function for calculating kinetic energy
def cal_kinetic_energy(m, v):
# kinetic energy formula
    KE = 0.5 * m * v**2
return KE

# main function
def main():
# input mass from user
    m = input("Please enter the mass (kg): ")
# input velocity from user
	v = input("Please enter the velocity (m/s): ")

# call kinetic energy method
print( cal_kinetic_energy(m, v), "is the calculated kinetic energy")

# call main
main()

but

Syntax errors detected :

Line 5:
return KE
^
SyntaxError: ‘return’ outside function

What is the problem? The syntax tells you exactly what the problem is, and even at which line.

1 Like

You may either indent that offending line so that it’s aligned with KE = 0.5 * m * v**2

def cal_kinetic_energy(m, v):
# kinetic energy formula
    KE = 0.5 * m * v**2
    return KE

Alternatively, simply return the computed value

def cal_kinetic_energy(m, v):
# kinetic energy formula
    return 0.5 * m * v**2