[codecademy] 3.CONDITIONALS AND CONTROL FLOW
CONDITIONALS AND CONTROL FLOW
Conditionals & Control Flow 1/15
Go With the Flow
분기에 대해서 배울 것임
샘플코드를 실행하여 분기가 되는것을 확인해보아라
문제
Check out the code in the editor. You'll see the type of program you'll be able to write once you've mastered control flow. Click Save & Submit to see what happens!
Conditionals & Control Flow 2/15
Compare Closely!
비교연산자 6가지
같다 ==
같지않다 !=
~보다 크다 <
~와 같거나 크다<=
~보다 작다 >
~보다 작거나 같다 >=
주의. ==와 =는 다르다, =는 변수를 세팅할때 사용
문제
Set each variable to True or False depending on what you think the result will be. For example, 1 < 2 will be True, because one is less than two.
Set bool_one equal to the result of 17 < 328
Set bool_two equal to the result of 100 == (2 * 50)
Set bool_three equal to the result of 19 <= 19
Set bool_four equal to the result of -22 >= -18
Set bool_five equal to the result of 99 != (98 + 1)
Conditionals & Control Flow 3/15
Compare... Closelier!
비교연산자를 좀더 가깝게
앞장에서 해봤던것을 바탕으로 응용
문제
Let's run through the comparators again with more complex expressions. Set each variable to True or False depending on what you think the result will be.
Set bool_one to the result of (20 - 10) > 15
Set bool_two to the result of (10 + 17) == 3**16
Set bool_three to the result of 1**2 <= -1
Set bool_four to the result of 40 * 4 >= -4
Set bool_five to the result of 100 != 10**2
Conditionals & Control Flow 4/15
How the Tables Have Turned
이예제를 통하여 True와 False의 비교결과가 세팅되는것을 배운다
3 < 5라는 명제가 True이기 때문에 bool_one에는 True가 세팅된다
문제
For each boolean value in the editor, write an expression that evaluates to that value.
Remember, comparators are: ==, !=, >, >=, <, and <=.
Use at least three different ones!
Don't just use True and False! That's cheating!
Conditionals & Control Flow 5/15
To Be and/or Not to Be
Boolean 연산자 and, or, not 세개의 특징
and : 각각의 상태가 True인것을 체크
or : 어떤 하나라도 True인것을 체크
not : 현재상태의 반대로 전환 True->False, False->True
문제
Look at the truth table in the editor. Don't worry if you don't completely get it yet—you will by the end of this section!
Click Save & Submit to continue.
Conditionals & Control Flow 6/15
and
and연산자
A and b -> a,b 모두 True여야 True를 얻을 수 있다 그렇지 않을경우 False
문제
Let's practice with and. Assign each variable to the appropriate boolean value.
Set bool_one equal to the result of False and False
Set bool_two equal to the result of -(-(-(-2))) == -2 and 4 >= 16**0.5
Set bool_three equal to the result of 19 % 4 != 300 / 10 / 10 and False
Set bool_four equal to the result of -(1**2) < 2**0 and 10 % 10 <= 20 - 10 * 2
Set bool_five equal to the result of True and True
Conditionals & Control Flow 7/15
or
or연산자
A or b -> a,b 둘중하나가 True여야 True를 얻을 수 있다, 둘다 False일 경우 False
문제
Time to practice with or!
Set bool_one equal to the result of 2**3 == 108 % 100 or 'Cleese' == 'King Arthur'
Set bool_two equal to the result of True or False
Set bool_three equal to the result of 100**0.5 >= 50 or False
Set bool_four equal to the result of True or True
Set bool_five equal to the result of 1**100 == 100**1 or 3 * 2 * 1 != 3 + 2 + 1
Conditionals & Control Flow 8/15
not
not연산자
현재 boolean상태를 반대로 전환
문제
Let's get some practice with not.
Set bool_one equal to the result of not True
Set bool_two equal to the result of not 3**4 < 4**3
Set bool_three equal to the result of not 10 % 3 <= 10 % 2
Set bool_four equal to the result of not 3**2 + 4**2 != 5**2
Set bool_five equal to the result of not not False
Conditionals & Control Flow 9/15
This and That (or This, But Not That!)
이거저거 저거이거 다섞었을때
not은 첫번째에 평가된다
and는 다음번에 평가된다
or은 마지막에 평가된다
문제
Assign True or False as appropriate for bool_one through bool_five.
Set bool_one equal to the result of False or not True and True
Set bool_two equal to the result of False and not True or True
Set bool_three equal to the result of True and not (False or False)
Set bool_four equal to the result of not not True or False and not True
Set bool_five equal to the result of False or not (True and True)
Conditionals & Control Flow 10/15
Mix 'n' Match
섞어 매치-> 연산자를 섞어서 연습
문제
This time we'll give the expected result, and you'll use some combination of boolean operators to achieve that result.
Remember, the boolean operators are and, or, and not. Use each one at least once!
Conditionals & Control Flow 11/15
Conditional Statement Syntax
조건문
if문으로 분기하는것을 배울 수 있다
if문 옆에 조건이 True일경우 아래 print문이 출력된다
문제
If you think the print statement will print to the console, set response equal to 'Y'; otherwise, set response equal to 'N'.
Conditionals & Control Flow 12/15
If You're Having...
조건문과 함수
if문은 함수의 return값에 따라도 분기된다
물론 함수안에서도 조건문으로 분기가능
문제
In the editor you'll see two functions. Don't worry about anything unfamiliar. We'll explain soon enough.
Replace the underline on line 2 with an expression that returns True.
Replace the underline on line 6 with an expression that returns True.
If you do it successfully, then both "Success #1" and "Success #2" are printed.
Conditionals & Control Flow 13/15
Else Problems, I Feel Bad for You, Son...
else문
else문은 if문과 짝을 이루어 사용되는 문이다, 단일로 사용불가능
if문에서 분기되지 못했을때 무조건 else문이 호출되는 분기 문법
문제
Complete the else statements to the right. Note the indentation for each line!
Conditionals & Control Flow 14/15
I Got 99 Problems, But a Switch Ain't One
elif문
각각의 조건으로 여러가지 분기구문을 만들어 낼 수 있다
문제
01. On line 2, fill in the if statement to check if answer is greater than 5.
02. On line 4, fill in the elif so that the function outputs -1 if answer is less than 5.
Conditionals & Control Flow 15/15
The Big If
마무으리 , 복습
비교연산자
문제
Write an if statement in the_flying_circus(). It must include:
01. if, elif, and else statements;
02. At least one of and, or, or not;
03. A comparator (==, !=, <, <=, >, or >=);
04. Finally, the_flying_circus() must return True when evaluated.
Don't forget to include a : after your if statements!
PygLatin 1/11
Break It Down
알파벳(String) 가지고 놀꺼임
문제
When you're ready to get coding, click Save and Submit. Since we took the time to write out the steps for our solution, you'll know what's coming next!
PygLatin 2/11
Ahoy! (or Should I Say Ahoyay!)
기본적인 message 출력
문제
Please print the phrase "Pig Latin".
PygLatin 3/11
Input!
입력방법
문제
01. On line 4, use raw_input("Enter a word:") to ask the user to enter a word. Save the results of raw_input() in a variable called original.
02. Click Save & Submit Code
03. Type a word in the console window and press Enter (or Return).
PygLatin 4/11
Check Yourself!
문자열에 대한 체크방법
문제
Write an if statement that verifies that the string has characters.
01. Add an if statement that checks that len(original) is greater than zero. Don't forget the : at the end of the if statement!
02. If the string actually has some characters in it, print the user's word.
03. Otherwise (i.e. an else: statement), please print "empty".
You'll want to run your code multiple times, testing an empty string and a string with characters. When you're confident your code works, continue to the next exercise.
PygLatin 5/11
Check Yourself... Some More
문자열체크 더더더더
문제
Use and to add a second condition to your if statement. In addition to your existing check that the string contains characters, you should also use .isalpha() to make sure that it only contains letters.
Don't forget to keep the colon at the end of the if statement!
PygLatin 6/11
Pop Quiz!
만든코드를 테스트해보시오
문제
Take some time to test your current code. Try some inputs that should pass and some that should fail. Enter some strings that contain non-alphabetical characters and an empty string.
When you're convinced your code is ready to go, click Save & Submit to move forward!
PygLatin 7/11
Ay B C
이제부터 문자열을 가지고 바꿀 장난질을 시작할꺼임
문제
Create a variable named pyg and set it equal to the suffix 'ay'.
PygLatin 8/11
Word Up
글자 끌어올리기
글자 소문자로 변환하는 함수
문자열에 대한 index 위치
주의. 항상 0부터 시작한다
문제
Inside your if statement:
01. Create a new variable called word that holds the .lower()-case conversion of original.
02. Create a new variable called first that holds word[0], the first letter of word.
PygLatin 9/11
Move it on Back
문자열 연산
문자열은 위와 같이 변수 더하듯(주의. 숫자는 문자열로 형변환하여 더해야함) 더하여 출력 및 저장이 가능하다
문제
On a new line after where you created the first variable:
Create a new variable called new_word and set it equal to the concatenation of word, first, and pyg.
PygLatin 10/11
Ending Up
엔딩끌어올리기
s[0]은 문자열중 문자하나 [0]의 위치를 사용하겠다는 의미이다.
s[1:4]는 해당 문자열의 s의 index인 1~4까지 사용하겠다는 의미이다. print를 사용하였으니 출력하겠다는의미
문제
Set new_word equal to the slice from the 1st index all the way to the end of new_word. Use [1:len(new_word)] to do this.
PygLatin 11/11
Testing, Testing, is This Thing On?
테스트하고 마무으리
문제
When you're sure your translator is working just the way you want it, click Save & Submit Code to finish this project.
참고 : codecademy
'프로그래밍 > Python' 카테고리의 다른 글
[codecademy] 4. FUNCTIONS (314) | 2015.09.05 |
---|---|
[codecademy] 5.LISTS & DICTIONARIES (319) | 2015.08.28 |
Python 시리얼 출력(serial 통신) (4) | 2015.07.24 |
python using ocr(python을 이용하여 ocr, 이미지 프로세싱) (6) | 2015.07.05 |
python 람다(Lambda) (4) | 2015.06.20 |