본문 바로가기

Programming

(38)
22.11.20 Challenge_Bounding Off Scribes 주말에 꼭 알맞게 Chanllege다! Challenge Goals - Scribe should bound off canvas edges - Vertical wall : Make the x direction negative - while y direction keeps going on - Horizontal wall : Make the y direction negative - while x direction keeps going on Hints! - You may want to modify the "hitsWall" function in the canvas to provide more information 우선 hitsWall 함수를 아래와 같이 변경하였다. 4개의 축에 부딪혔을 때 서로 다른 문구를 반..
22.11.18 Control Flow - For - For 구문 꽤나 직관적이고 지금까지 당연하게 사용해왔던 For 구문. >>> myList = [1,2,3,4,5] >>> for item in myList: ... print(item) ... 1 2 3 4 5 - Pass Statement 아래와 같이 pass 사용 시 아무 일도 나타나지 않는다! >>> animalLookup = { ... 'a':['aardvark','antelope'], ... 'b':['bear'], ... 'c':['cat'], ... 'd':['dog'], ... } >>> for letter,animals in animalLookup.items(): ... pass - Continue Statement 아래와 같이 continue를 이용하여 특정 조건을 만족할 시 특정..
22.11.17 Control Flow - While While은 자칫 잘못 썼다가는 무한루프로 빠지기 십상이기 때문에 적절한 control statements를 함께 써줄 필요가 있다. 오늘은 해당 control statements를 한 번 보자. - While 구문 아래 구문은 무한 루프는 아니지만 화면은 'still waiting'으로 가득찰 것이다. 위로 올라갈 수 없다. 아무리 올라가도 'still waiting' 뿐이다. Python은 상당히 빠르고 2초는 굉장히 길기 때문이다. from datetime import datetime wait_until = datetime.now().second + 2 #현재로부터 2초 뒤 while datetime.now().second != wait_until: print('still waiting') prin..
22.11.15 Control Flow - If and Else If + Else statements을 Elif statements로 간소화 가능, Single line if statements로 더 간소화 가능! - If + Else 구문 >>> for n in range(1,10): ... if n%3 == 0: ... print('FizzBuzz') ... else: ... if n%5 ==0: ... print('Fizz') ... else: ... print(n) ... 1 2 FizzBuzz 4 Fizz FizzBuzz 7 8 FizzBuzz - Elif 구문 >>> for n in range(1,10): ... if n%3 == 0: ... print('FizzBuzz') ... elif n%5 == 0: ... print('Fizz') ... else:..
22.11.13 Challenge_Structuring Scribes(3) 세 개의 data set이 있다. 타입은 dictionaries. 첫 번째, [15,15]에서 시작해서 30'의 각도로 100번 이동. {'degree' : 30, 'position' : [15,15], 'instructions' : [ {'function':'forward', 'duration':100} ]} 두 번째, [0,0]에서 시작해서 145'의 각도로 10번 이동 후, 오른쪽으로 20번 이동 후, 아래로 2번 이동. {'degree' : 135, 'position' : [0,0], 'instructions' : [ {'function':'forward', 'duration':10}, {'function':'down', 'duration':2}, {'function':'right', 'durat..
22.11.12 Challenge_Structuring Scribes(2) 오늘은 정답지를 봐야겠다. 함수를 보고, 한 번 코드 돌려보고, 출제자의 의도가 무엇인지 파악하고, object를 고쳐 보아야겠다! Solution의 결과창...... canvas = Canvas(30, 30) scribes = [ #3개의 Key가 존재하는 dictionaries를 3개 정의 {'degrees': 30, 'position': [15,15], 'instructions': [ {'function':'forward', 'duration': 100} ]}, {'degrees': 135, 'position': [0, 0], 'instructions': [ {'function':'forward', 'duration': 10}, {'function':'down', 'duration': 2}, {'f..
22.11.07 Challenge_Structuring Scribes Challenge! Structuring Scribes - Create a data structure that holds information about each scribe (starting points, directions, names, movements) - Write a function that creates and moves scribes based on your data structure - Canvas definition is not necessarily required, but you can if you want - Make a list of dictionary objects that each dictionary defines scribe - If you want it to go forwa..
22.11.06 Basic Data Structures - Lists & Dictionaries Comprehensions 오늘 좀 고급 문법을 배울 것 같다... 뭔가 pythonic 문법... 오늘 남친이랑 헤어져서 컨디션이 별로 좋지 않은데 후딱 하고 자야겠다. ㅠ Lists Comprehensions : Lists의 생성/수정/조작을 간단히 한 줄로 구현하고자 하는 문법이다. - for loop를 통해 기존 lists를 새로운 lists로 재탄생시킬 수 있다. >>> myList = [1,2,3,4,5] >>> DoublemyList = [2*item for item in myList] #예시 1; 값을 몇 배로 불리기 >>> print(DoublemyList) [2, 4, 6, 8, 10] >>> FilteredList = [item for item in myList if item % 10 < 2] #예시 2; 값 ..