① 프로그래밍을 하다보면 종종 사용자의 입력이 필요한 경우가 존재한다.
㉠ input() 함수는 사용자의 입력이 들어올 때까지 프로그램을 중지시키고, 받은 입력을 리턴하는 함수이다.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
print(input()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
네모 | |
네모 |
㉡ input() 함수는 생략할 수 있는 매개변수 한 개를 사용한다.
ⓐ 매개변수로 받은 값을 프롬프트 창에 출력한다. 즉, 사용자 입력과 매개변수는 독립되어 있다.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
input("잠깐만, 할 말이 있어.") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
잠깐만, 할 말이 있어. |
ⓑ 일반적으로 사용자에게 입력에 대한 지시를 하기 위해 사용한다.
㉢ 파이썬은 input() 함수의 모든 리턴 값은 문자열이다.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
answer = input("1 + 3은?? : ") | |
print(type(answer)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 + 3은?? : 4 | |
<class 'str'> |
㉣ 따라서 입력값을 다른 형태로 이용하려면 형변환(type casting)을 이용해야 한다.
Python 형변환(Type Casting)
① 서로 다른 데이터 타입을 혼용해서 사용할 경우 다음과 같은 오류가 발생할 수 있다. <>HTML 삽입 미리보기할 수 없는 소스 ㉠ 이러한 경우는 데이터의 타입을 통일시켜주면 문제가 해결된다.
hemahero.tistory.com
ⓐ 형변환을 하지 않고 임의대로 이용하면 오류가 발생할 수 있다.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
answer = input("1 + 3은?? : ") | |
print(answer + 3) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 + 3은?? : 4 | |
Traceback (most recent call last): | |
File "example.py", line 2, in <module> | |
print(answer + 3) | |
TypeError: can only concatenate str (not "int") to str |
ⓑ 필요한 경우 형변환을 해줘야 한다.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
answer = input("1 + 3 = x : ") | |
print("x에 3을 더한 값은",int(answer) + 3) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 + 3 = x : 4 | |
x에 3을 더한 값은 7 |
'All about Python > 기본' 카테고리의 다른 글
13. Python 함수(Functions) (0) | 2022.07.29 |
---|---|
12. Python While 반복(Loops) (0) | 2022.07.27 |
10. Python 딕셔너리(Dictionaries) (0) | 2022.07.17 |
9. Python IF 문(IF STATESMENTS) (0) | 2022.07.12 |
8. Python for문 (0) | 2022.05.27 |