3. Functions without parameters
def keyword.3.1. Functions that do something
def keyword and has empty parentheses.def line has a colon at the end and the code for the definition is indented.show_welcome().from microbit import *
def show_welcome():
display.scroll("Hello mb user!", delay=80)
show_welcome()
Tasks
Write a function called
scroll_namethat scrolls your name.Write a function called
countdownthat counts down from 5 to 1, showing each number.
Write a function called scroll_name that scrolls your name.
from microbit import *
def scroll_name():
display.scroll("my name", delay=80)
scroll_name()
Write a function called countdown that counts down from 5 to 1, showing each number.
from microbit import *
def countdown():
for num in range(5, 0, -1):
display.show(num)
sleep(300)
display.clear()
countdown()
3.2. Functions that return a value
from microbit import *
def is_right_tilt():
if accelerometer.get_x() > 300:
return True
else:
return False
while True:
display.scroll(is_right_tilt())
sleep(500)
Tasks
Write a function called
is_left_tiltthat returns True or False.Write a function called
is_coldthat returns True when the temperature is below 20 Celsius, otherwise False.Write a function called
get_fahrenheitthat returns the temperature in degress Fahrenheit.
Write a function called is_left_tilt that returns True or False.
from microbit import *
def is_left_tilt():
if accelerometer.get_x() < -300:
return True
else:
return False
while True:
display.scroll(is_left_tilt())
sleep(500)
Write a function called is_cold that returns True when the temperature is below 20 Celsius, otherwise False.
from microbit import *
def is_cold():
if temperature() < 20:
return True
else:
return False
while True:
display.scroll(is_cold())
sleep(500)
Write a function called get_fahrenheit that returns the temperature in degress Fahrenheit.
from microbit import *
def get_fahrenheit():
celsius = temperature()
fahrenheit = (celsius * 1.8) + 32
return fahrenheit
while True:
display.scroll(get_fahrenheit())
sleep(500)