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

Veröffentlicht am: 06 August 2021
auf dem Kanal: Book Simplifiers
117
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)


Auf dieser Seite können Sie das Online-Video Sequence - Introduction to Python: Absolute Beginner Module 2 Video 6 mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer Book Simplifiers 06 August 2021 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 117 Mal angesehen und es wurde von 2 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!