파이썬 오류
조회수 879회
import urllib.request
price=99
while price > 5:
page=urllib.request.urlopen(
"http://beans-r-us.appspot.com/prices.html")
text=page.read().decode("utf-8")
start=text.find(">$")+2
end=start+4
price=text[start:end]
print(price)
if float(price) > 5.5:
print("Wait")
else:
print("Buy")
이 프로그램을 올바르게 고치는 방법은 알고 있는데 이 프로그램이 무엇이 잘못되서 오류가 생기는지 알고 싶습니다.
1 답변
-
처음에 price는 99로 초기화되어, 숫자 타입입니다. 그런데 중간에
price=text[start:end]
을 실행하면서 price의 타입이 string이 되어버리지요.따라서
while price > 5
에서 문자열과 정수를 비교할 때 에러가 발생합니다.-
(•́ ✖ •̀)
알 수 없는 사용자
-
댓글 입력