👉 기본 환경

- Language: Python

- IDE: Replit

 

 

⌨️ 코드

1
2
3
4
5
if height>=120:
print("Yes!");
else :
  print("No");
 
 

 

 

🖨️오류

IndentationError: expected an indented block

 

 

📡 원인

if 조건이 true일 경우, 실행되어야하는 부분이 들여쓰기로 작성되어야 인식

 

 

📰 해결 방법

1
2
3
4
5
if height>=120:
  print("Yes!");
else :
  print("No");
 
 

true일 경우, 실행되어야 하는 부분을 들여쓰기하여 작성

 

이 글은 Dr. Angela Yu의 [Python_Bootcamp]를 수강하며 정리한 글입니다.

 

👉 기본 환경

- Language: Python

- IDE: Replit

 

 

f-String

- 문자열 Formatting 기법

- 여러 자료형을 str과 함께 사용할 경우, TypeError 발생

- 여러 자료형을 str로 형변환 해주는 불편함을 줄임

 

1
2
3
4
5
height = input("enter your height in m: ")
weight = input("enter your weight in kg: ")
 
print(f"your height is {height}m and weight is {weight}");
 
 
 

- {}를 활용하여 변수 삽입, 함수 호출 등을 할 수 있음

 

 

 

📚 참고 자료

 

파이썬의 f-string으로 문자열 포맷팅하기

Engineering Blog by Dale Seo

www.daleseo.com

 

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

[Python_Bootcamp] 리스트 랜덤 추출  (0) 2023.08.16
[Python_Bootcamp] Data Structures: List  (0) 2023.08.13

👉 기본 환경

- Language: Python

- IDE: Replit

 

 

⌨️ 코드

1
2
print(int(weight)/int(height)**2);
 
 
 

 

 

🖨️오류

ValueError: invalid literal for int() with base 10: '1.8'

 

 

📡 원인

int 타입의 값에 1.8 입력

 

 

📰 해결 방법

1
2
print(float(weight)/float(height)**2);
 
 
 

변수 weight, height 값이 변환 시, 실수일 경우 int()가 아닌 float()으로 형 변환

 

👉 기본 환경

- Language: Python

- IDE: Replit

 

 

⌨️ 코드

1
2
3
answer = input("What is your number?\n");
temp = answer / 10;
 
 
 

 

 

🖨️오류

TypeError: unsupported operand type(s) for /: 'str' and 'int'

 

 

📡 원인

자료형이 str과 int 사이에는 / 연산자를 지원하지 않음

 

 

📰 해결 방법

1
2
3
4
answer = input("What is your number?\n");
temp1 = int(answer) / 10;
temp2 = float(answer) / 10;
 
 
 

str을 int() 또는 float()로 / 연산자가 지원하는 형태로 변환

 

👉 기본 환경

- Language: Python

- IDE: Replit

 

 

⌨️ 코드

1
2
3
len_answer = len(input("What is your name?\n"));
print("len: " + len_answer);
 
 
 

 

 

🖨️오류

TypeError: can only concatenate str (not "int") to str

 

 

📡 원인

int와 str은 결합하지 못하고, str과 str간의 결합만 가능

 

 

📰 해결 방법

1
2
3
len_answer = len(input("What is your name?\n"));
print("len: " + str(len_answer));
 
 
 

int값, len_answer를 str로 형변환

 

 

cf. python에서 타입 확인하는 방법

1
2
3
4
5
len_answer = len(input("What is your name?\n"));
 
print(type(len_answer)); # <class 'int'>
print(type(str(len_answer))); # <class 'str'>