TIL: Python Basics Day 6 - function, while loop

이다연·2020년 11월 25일
0

Udemy Python Course

목록 보기
6/64

Functions

we've already used built in functions such as print(), len(), int() ...however we can make our own funcitons too.

#Defining Functions
def my_function():
    print("Bye") #do this

#Calling Functions
my_function()

Exercise 6. 1

reeborg's world

Code Blocks

tab over space
python recommend using space..!

While Loops

for loop is definite repeatition with list, range
while loop continue repeating as long as a condition is true
-> can be dangerous as it can fall into infinite loop

while something_is_ture
	do this

This mini robot reminds me of 2016 robot event parttime job. I had no idea that I would be into programming back then.

Exercise 6.2 Hurdles

Exercise 6.3 Hurdles with height

It took nearly an hour to solve this task. 
Even though I approached it with logic flow chart.
Difference btw my code and angela's code is, 
she wrote the while loop inside jump() so it can be repeated only when the code reaches jump(). 
It keeps the code much more readable and simple!!

#<mycode>
def turn_right():
    turn_left()
    turn_left()
    turn_left()
def jump():
    turn_right()
    move()
    turn_right()
    move()
    
while not at_goal():
    while wall_on_right() and wall_in_front:  
            if wall_in_front():
                turn_left()
            elif wall_on_right:
                if at_goal():
                    done()
                else:
                    move()  
    jump()


#<angela's code>

def turn_right():
    turn_left()
    turn_left()
    turn_left()
def jump():
    turn_left()
    while wall_on_right():
        move()
    turn_right()
    move()
    turn_right()
    while front_is_clear():
        move()
    turn_left()
    
while not at_goal():
    if wall_in_front():
        jump()
    else:
        move()
        	

Final Project : Escaping the Maze

Algorithm: following the right edge of the wall.

def turn_right(): 
    turn_left()
    turn_left()
    turn_left()

    #<mycode>
while not at_goal():
    if wall_on_right():
        if wall_in_front():
            turn_left()
        elif front_is_clear():
            move()
 
    else:
        turn_right()
        move()
     
        #<angela's code>
while not at_goal():
    if right_is_clear():
        turn_right()
        move()
    elif front_is_clear():
        move()
    else:
        turn_left()

profile
Dayeon Lee | Django & Python Web Developer

0개의 댓글