Sequence - Introduction to Python: Absolute Beginner Module 2 Video 6

Publié le: 06 août 2021
sur la chaîne: Book Simplifiers
119
2

Concept: Sequence
Considering Sequence
In programming, sequence refers to the order that code is processed. Objects in Python, such as variables and functions, are not available until they have been processed.

Processing sequence flows from the top of a page of code to the bottom. This often means that Function definitions are placed at the beginning of a page of code.

In the sample below, the function hat_color cannot be accessed since it is initialized after it is called at the bottom of the code.

have_hat = hat_available('green')

print('hat available is', have_hat)

def hat_available(color):
hat_colors = 'black, red, blue, green, white, grey, brown, pink'
return(color.lower() in hat_colors)
This results in an error - the code flows from top to bottom is in the incorrect sequence

NameError: name 'hat_available' is not defined
In the statement have_hat = hat_available('green') the function hat_available() needs to be called after the function has been defined

Note: an argument or variable is said to be hard coded when assigned a literal or constant value.
It is a good habit to avoid creating hard coded values in functions, such as
hat_colors = 'black, red, blue, green, white, grey, brown, pink'

Example
review and run code - note: fix error in the following "tasks" section
have_hat = hat_available('green')

print('hat available is', have_hat)

def hat_available(color):
hat_colors = 'black, red, blue, green, white, grey, brown, pink'
return Boolean
return(color.lower() in hat_colors)
Task 1
Change the Sequence to fix the NameError
[ ] fix the code sequence so the hat_available() function is available when called and the code runs without error
[ ] fix the sequence of the code to remove the NameError

have_hat = hat_available('green')

print('hat available is', have_hat)

def hat_available(color):
hat_colors = 'black, red, blue, green, white, grey, brown, pink'
return(color.lower() in hat_colors)


Sur cette page du site, vous pouvez voir la vidéo en ligne Sequence - Introduction to Python: Absolute Beginner Module 2 Video 6 durée heure minute seconde en bonne qualité , qui a été Téléchargé par l'utilisateur Book Simplifiers 06 août 2021, Partagez le lien avec vos amis et connaissances, sur youtube cette vidéo a déjà été regardée 119 fois et il a aimé 2 téléspectateurs. Bon visionnage!