This is a 100 Days challenge to learn a new language (Python). 100 Days of Code - The Complete Python Pro Bootcamp
I will post some notes to motivate myself to finish this challenge.
Function
Built-in and Custom Function
With custom function, we can wrap some reusable code into a function and we can use it later.
It provides a way to reduce repetition, and also it improves readability.
Ex:
# Define a custom function
def my_function():
# Do this
# Then do this
# Finally do this
print("This is my function")
# Use the custom function
my_function()
Result:
This is my function
Indentation
In Python, indentation is really important, but Space V.S. Tab? Reference
Spaces are the preferred indentation method.
Use 4 spaces per indentation level.
Ex:
# * means a space (demo purpose)
def my_function(sky):
****if sky == "clear":
********print("blue")
****elif sky == "cloudy":
********print("grey")
****print("bye")
While Loops
Be careful the infinite loop...
Ex:
num = 0
while num < 3:
print("this is inner of while loop")
num = num + 1
Result:
this is inner of while loop
this is inner of while loop
this is inner of while loop
Challenge - Reeborg - Hurdle 1
Ex: Link
# Built-in Func
def turn_left():
...
def move():
....
def at_goal():
..
def front_is_clear():
...
# Custom Func
# Using built-in Func to generate a turn_right custom Func
def turn_right():
turn_left()
turn_left()
turn_left()
def jump():
turn_left()
move()
turn_right()
move()
turn_right()
move()
turn_left()
# Keep going if robot has not reached its goal
while not at_goal():
# If there is no bar in front of it -> move
if front_is_clear():
move()
# Jump if there is a bar in front of it
else:
jump()
Challenge - Reeborg - Hurdle 4
Ex: Link
# Built-in Func
def turn_left():
...
def move():
...
def at_goal():
...
def front_is_clear():
...
def right_is_clear():
...
def turn_right():
turn_left()
turn_left()
turn_left()
def jump_as_high_as_wall():
turn_left()
move()
while not right_is_clear():
move()
turn_right()
def move_down_to_floor():
turn_right()
while front_is_clear():
move()
turn_left()
def jump():
# Up
jump_as_high_as_wall()
# Move
move()
# Down
move_down_to_floor()
while not at_goal():
# If there is no bar in front of it -> move
if front_is_clear():
move()
# Jump if there is a bar in front of it
else:
jump()
No comments:
Post a Comment