🚨

1118이상은 오류 리포트

1. if-else 문

if-else 문을 사용하여 점수가 90점 이상이면 합격, 90점 미만이면 불합격 출력
입력 예시
출력 예시
90
합격
89
불합격
score = input() if int(score) => 90: print('합격') else: print('불합격')
Python
복사

 오류 및 해석

SyntaxError: 문법상에 에러

Traceback(most recent call last) : 역추적
3번 줄이 마지막 이다.
SyntaxError: invalid syntax
if int(score) => 90: ^
Plain Text
복사
=> 은 잘못된 문법이다.
>=로 바꾸면 된다.

 완성 코드

score = input() if int(score) => 90: print('합격') else: print('불합격')
Python
복사

2. range 문

range 문을 사용하여 첫 번째로 입력받은 수 만큼 두 번째 a,b 의 더하기 반복
r = input() for i in range(1,r+1): a,b = map(int, input().split()) print("Case #"+str(r)+':',a+b)
Python
복사

 오류 및 해석

TypeError: 타입 에러

Traceback(most recent call last) : 역추적
3번 줄이 마지막 이다.
TypeError: can only concatenate str (not "int") to str
r = input() for i in range(1,r+1):
Python
복사
int로 받아주면 된다

 완성 코드

r = int(input()) for i in range(1,r+1): a,b = map(int, input().split()) print("Case #"+str(r)+':',a+b)
Python
복사

3. 랜덤으로 숫자 하나 뽑기

1~9까지 랜덤으로 숫자 하나를 뽑아서 출력하는 코드립니다. Random 모듈 사용
import random as r print(random.randint(1,9))
Python
복사

 오류 및 해석

NameError: 네임 에러

ERROR!
Traceback (most recent call last): File "<main.py>", line 2, in <module> NameError: name 'random' is not defined
Traceback(most recent call last) : 역추적
2번 줄이 마지막 이다.
NameError: name 'random' is not defined
랜덤 함수가 지정되지 않았다.
r로 랜덤 모듈을 불러왔으니 ramdom을 r로 바꿔주면 된다

 완성 코드

import random as r print(r.randint(1,9))
Python
복사

4. 코드가 몇초동안 실행 되고 있는지 확인

현재 코드가 몇초동안 실행 되고 있는지 확인 하는 모듈입니다.
import time print(time.clock())
Python
복사

 오류 및 해석

AttributeError: 속성 참조 또는 할당이 실패

ERROR!
Traceback (most recent call last): File "<main.py>", line 2, in <module> AttributeError: module 'time' has no attribute 'clock'
Traceback(most recent call last) : 역추적
2번 줄이 마지막 이다.
AttributeError: module 'time' has no attribute 'clock'
속성이 잘못 사용되었을때 나온다.
time에는 clock이라는 함수가 없다.
파이선 3.3이후로 삭제된 코드라서 저렇게 나온것이다. time.process_time()을 쓰면 정상적으로 기존 처럼 사용이 가능하다.

 완성 코드

import time print(time.process_time())
Python
복사

5. 리스트에서 최대 값 가져오기

지정된 리스트에서 최대값을 가져오는 코드 입니다.
tempList = [] first = max(tempList) tempList.append(1) second = max(tempList) print(f"추가전 최대: {first}, 추가후 최대: {second}")
Python
복사

 오류 및 해석

ValueError: 함수가 받은 파라미터가 올바른 자료형이나, 부적절한 값일 때

ERROR!
Traceback (most recent call last): File "<main.py>", line 2, in <module> ValueError: max() arg is an empty sequence
Traceback(most recent call last) : 역추적
2번 줄이 마지막 이다.
ValueError: max() arg is an empty sequence
max()에서 받은 리스트가 빈 시퀀스다
list에는 빈값만 있어서 저렇게 나왔다, list에 값을 추가해주면 된다.

 완성 코드

tempList = [0] first = max(tempList) tempList.append(1) second = max(tempList) print(f"추가전 최대: {first}, 추가후 최대: {second}")
Python
복사

6. 웹 서버 만들기

다음은 웹 서버를 만드는 코드다. /hello 에 접속하면 hello가 리턴 된다.
import flask app = flask.Flask(__name__) @app.get("/hello") def hello(): return "hello" app.run()
Python
복사

 오류 및 해석

ModuleNotFoundError: 모듈을 찾을 수 없을 때 발생하는 에러

ERROR!
Traceback (most recent call last): File "<main.py>", line 1, in <module> ModuleNotFoundError: No module named 'flask'
Traceback(most recent call last) : 역추적
1 번째 줄이 마지막이다.
ModuleNotFoundError: No module named 'flask'
flask 모듈을 찾을수 없다.
flask 모듈을 설치 해야된다. pip install flask 를 터미널에 입력 해야된다.

 완성 코드

import flask app = flask.Flask(__name__) @app.get("/hello") def hello(): return "hello" app.run()
Python
복사

7. list의 두번째 데이터를 출력 하기

1,2 가 있는 리스트에 2번째 데이터를 출력 해야된다
tempList = [1,2] print(tempList[2])
Python
복사

 오류 및 해석

IndexError: 리스트의 범위를 벗어난 인덱스에 접근하려 하는 경우 발생하는 에러

ERROR! Traceback (most recent call last): File "<main.py>", line 2, in <module> IndexError: list index out of range
Traceback(most recent call last) : 역추적
2 번째 줄이 마지막 줄이다.
IndexError: list index out of range
리스트가 범위에서 벗어난다.
print(tempList[2]) 를 출력 할려고 했을때 오류가 난 이유는 파이썬 리스트는 0번째부터 시작하기 때문에 -1하여 계산 해야된다.

 완성 코드

tempList = [1,2] print(tempList[1])
Python
복사

8. Hello World 출력

모든 프로그래밍의 기본인 Hello World를 출력 해볼려고 한다
print("Hello World)
Python
복사

 오류 및 해석

SyntaxError: 프로그램 구문이 잘못 쓰였을 경우 발생하는 에러

ERROR! Traceback (most recent call last): File "<main.py>", line 1 print("Hello World) ^ Traceback(most recent call last) : 역추적
1 번째 줄이 마지막 줄이다.
SyntaxError: unterminated string literal (detected at line 1)
문자열이 종료 되지 않았다.
print("Hello World) 가 출력 되지 않은 이유는 마지막에 따움표를 작성하지 않아서다

 완성 코드

print("Hello World")
Python
복사

9. 친구 불러오기

나의 친구들을 딕셔너리로 만들어서 key로 불러올려고 한다
friends = {"신지윤": "신지드", "김강연": "강잼민", "서민덕":"민바오", "김도영": "도영시치"} print(friends["이상은"])
Python
복사

 오류 및 해석

SyntaxError: 프로그램 구문이 잘못 쓰였을 경우 발생하는 에러

이상은 이라는 키를 불러 올려고 하는데 오류가 발생했다.
ERROR! Traceback (most recent call last): File "<main.py>", line 2, in <module> KeyError: '이상은'
Traceback(most recent call last) : 역추적
File "<main.py>", line 2, in <module>
2 번째 줄이 마지막 줄이다.
KeyError: '이상은'
이상은 이라는 키가 존재하지 않는다.
이 오류를 해결 하는 방법은 이상은 이라는 키와 값을 추가 해주면 된다.

 완성 코드

friends = {"신지윤": "신지드", "김강연": "강잼민", "서민덕":"민바오", "김도영": "도영시치", "이상은":"잘생김"} print(friends["이상은"])
Python
복사

10. 메모장 읽어오기

메모장에 있는 내용을 읽어올려고 한다.
with open('test.txt', 'r') as f: data = f.read() print(data)
Python
복사

 오류 및 해석

FileNotFoundError: 존재하지 않는 파일을 열려고 할 때 발생합니다.

test.txt
Traceback (most recent call last): File "<main.py>", line 1, in <module> FileNotFoundError: [Errno 2] No such file or directory: 'test.txt'
Traceback(most recent call last) : 역추적
File "<main.py>", line 1, in <module>
첫번재 줄이 오류가 난 곳이다.
FileNotFoundError: [Errno 2] No such file or directory: 'test.txt'
test.txt라는 파일이 존재하지 않는다
이 오류를 해결 할려면 test.txt를 생성 해주면 된다, 또는 추가 모드로 읽어 주면 된다.
추가 모드는 파일을 추가 해준다.

 완성 코드

with open('test.txt', 'a') as f: data = f.read() print(data) 또는 with open('test.txt', 'w') as f: data = f.read() print(data) 에서 파일을 추가 해주면 된다.
Python
복사