Notice
Recent Posts
Recent Comments
Link
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

JINIers

[Python 100 day challenge ] Day 6 - 허들 본문

공부/Python

[Python 100 day challenge ] Day 6 - 허들

JINIers 2024. 4. 20. 22:03

Day 6는 허들을 뛰어야했다.

이런 허들임

 

이걸 하기 위해 function을 배웠다


# function

 

# def : 함수생성 & 함수 정의(defining function)

def 의 기본 틀

def my_function():
    print("hello")
    print("bye")
    
my_function()			# 호출함수

마지막 줄은 설정한 함수를 호출한다.

호출함수(calling functions) 라고 한다.

 


함수 설정 할 때 들여쓰기가 중요하다.

# indentation

tab으로 들여쓰기 : 4칸

스페이스바로 들여쓰기 : 1칸


# while loops

while 기본 틀

while something_is_true:
  # do something repeatedly

 

for가 어떤 범위에서 반복하는 것이라면 while은 특정조건이 참일 경우에만 작동한다.

False일 땐 작동하지 않는다.


위에 function 기본개념을 배우고 허들을 4단계까지 짠다!

#step 1

나 ▼

def turn_right():
    turn_left()
    turn_left()
    turn_left()
    
def jump():
  turn_left()
  move()
  turn_right()
  move()
  turn_right()
  move()
  turn_left()

for step in range(6):
    move()
    jump()

안젤라 ▼

number_hurdles = 6
while number_hurdles > 0:
    move()
    jump()
    number_hurdles -= 1
    print(number_hurdles)

# step 2

두번째는 랜덤으로 목적지 위치가 바뀐다.

목적지까지만 도달하면 된다.

while not at_goal():
  move()
  jump()


# step 3

허들을 넘어야할 수도 있고 그냥 움직여야할 수도 있다.

while not at_goal():
  if wall_in_front():
      jump()
  elif front_is_clear():
      move()

 

아니면

while not at_goal():
  if wall_in_front():
    jump()
  else:
    move()


# step 4

허들 4.. .너무 어려웠다..

이건 허들이 아니고 탑이잖아요.

진짜..

 

하...

진짜 개띠용띠용 너무 어려워

무한 실패함

 

실패작 #1

while not at_goal():
  if wall_in_front():
      turn_left()
      if wall_on_right():
          move()
      else:
          turn_right()
  elif front_is_clear() and right_is_clear():
      jump()
  elif front_is_clear():
      move()

깃발에 닿지도 못해 돌고돌아

다시 돌고돌아 제자리로 오겠지. 넵 알겠습니다.

 

실패작 #2

def turn_right():
  turn_left()
  turn_left()
  turn_left()
def jump():
  if front_is_clear() and right_is_clear():
      move()
      turn_right()
  elif wall_in_front():
      turn_left()
  else:
      move()

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

 

응 이것도 돌고돌았다..

 

허들 4는 뭐냐 저거 jump()를 손봐주고 해야함..

이렇게..

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()

하다가 도저히 안될 것 같으면 그냥 강의 틀어놓고 안젤라랑 같이해야함

내가 학생도 아니고 일주일내내 고민하고 시도해도 안풀리는데 그럼 언제 실무하나요.

그래서 한 3일정도 지나도 못풀면 그냥 강의 틀어놓고 안젤라와 같이 하기로 함

안그러면 진짜

 

코드 갈무리가 필요했다.. 절실하게..

 

복잡하게 생각할 필요 없다고 해놓고 개복잡하게 생각함

어려우면 일단 알고리즘을 먼저 그려놓고 그대로 작성하는 것도 하나의 방법인 것 같다.


# maze

step 4까지 했으면 이제 미로 탈출을 해야함

얘는 더 어렵다. 봇봇이가 방향을 지멋대로 빙글뱅글 랜덤으로 설정한다.

 

안젤라 Tip!

오른쪽 가장자리를 따라가도록 하고 가능하면 우회전 하라고 했음

그리고 점프를 쓰지 말라고 했다.

while not at_goal():
  if right_is_clear() and not wall_in_front():
    move()
  elif wall_in_front() and wall_on_right():
    turn_left()
  elif wall_in_front() and right_is_clear():
    turn_right()

이렇게 했을 때

1보만 가면 되는데...

왜.. 가질 못하시나요... 웨...

앞으로 가시라고요.

하 못들은 척 하지말고 가시라고요ㅜㅜ..

근데 안감 당연함 코드를 잘못 짬

 

엉엉울면서(안욺) 안젤라와 함께했다.

안젤라 당신은 한줄기 빛..

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

while not at_goal():
   if right_is_clear():
      turn_right()
      move()
   elif front_is_clear():
      move()
   else:
      turn_left()

 

얘도 갈무리가 필요했다..

이해하고 하기 위한..

처음엔 이렇게 짜서 했었는데 너무 복잡하게 생각한거였음

굳이 이렇게 복잡하게 생각해서 할 필요가 없었다.

아래는 강의 듣고 안젤라가 짜준 코드대로 다시 생각해서 정리한 것


어렵...

어렵다...

 

하..하지만 능히 해낼것임..

일단 day 10까지만이라도 열심히 해보자는 마음가ㅓ짐을 가지고 하고 있다고..

그래.. 일단ㅇ 해 ..ㅜㅜ

하다보면 또 괜찮아질거야 웅.

아 미로 코드는 중간에 디버깅 해야하는 부분이 있는데 그건 day15까지 한 뒤에 다시해보라고 했다.

15까지 열심히 들으면 한번 해봐야겠다

'공부 > Python' 카테고리의 다른 글

Day 8 cipher  (1) 2024.06.17
Day 8 def  (0) 2024.06.17
[Python 100 day challenge ] Day 5 - for  (0) 2024.03.27
[Python 100 day challenge ] Day 4  (0) 2024.03.27
[Python 100 day challenge ] Day 3 - 2  (1) 2024.03.15
Comments