Clearly speed
is a parameter for the function, so pass it to the function as an argument, not via a global variable.
def journey(knots):
'''Convert knots to kilometres per day'''
return round(knots * 1.852 * 24)
>>> speed = 5 # in knots
>>> print("Ship travels {} kilometres in one day".format(journey(speed)))
Ship travels 222 kilometres in one day
>>> print(journey(10))
444
The key thing to know is that you call the function which is achieved by using the parentheses, ()
. Any arguments that you need to pass to the function are listed in the parentheses, in this case a single argument named knots
. So, to call the function use journey(5)
to pass 5
as the value of the knots
argument. You can use variables names to pass values to the function, i.e. journey(speed)
will pass the value of speed
to the function.
If you want to remember the value returned by a function you can assign it to a variable:
>>> distance = journey(10)
>>> distance
444
solved How to use a function after definition?