File "C:\tv.py", line 20, in set_channel
self.channel = new_channel
RuntimeError: maximum recursion depth exceeded
첫 OOP 문제를 시도하고 있습니다. 내 교과서의 문제는 다음과 같습니다 :OOP를 할 때마다 '최대 재귀 깊이 초과'
"개체를 만들어서 텔레비전을 시뮬레이트하는 프로그램을 작성하십시오. 사용자가 볼륨을 높이거나 낮추고 채널 번호를 설정할 수있게하십시오. 채널 및 볼륨 값을 확인하십시오. 유효한 범위 내에서 머물러 라. "
내 질문은 :
내 구조가 광범위하게 맞습니까? (inc/dec 볼륨 등의 메소드를 만들기 위해 선택했습니다)
왜 재귀 오류가 발생합니까? 나는 그것이 내가 어떤 종류의 무한 루프에 있음을 의미한다고 생각하지만, 나는 그것을 절대 볼 수 없다.
내 미완성 '솔루션'
# Television with Classes
# User able to enter a channel number
# and increase/decrease the volume
class Television(object):
"""A TV set."""
def __init__(self, channel = 1, volume = 20):
self.channel = channel
self.volume = volume
def get_channel(self):
"""Return current channel."""
return self.channel
def set_channel(self, new_channel):
"""Set the channel."""
if 1 <new_channel> 5:
print "Invalid Channel. Must be between 1-5."
else:
self.channel = new_channel
channel = property(get_channel, set_channel)
def show_volume(self):
"""Return current volume."""
return self.volume
def inc_volume(self):
"""Increase the volume by 1 unit."""
if self.volume >= 20:
print "Volume is at maximum: ", get_volume, "\n"
else:
self.volume += 1
def dec_volume(self):
"""Decrease the volume by 1 unit."""
if self.volume <= 0:
print "Volume is at minimum: ", get_volume, "\n"
else:
self.volume -= 1
sony = Television()
print "Current channel: ", sony.channel
print "Changing channel to 3..."
sony.channel(3)
print "Current channel: ", sony.channel
print "Current volume: ", self.volume
print "Increasing volume..."
sony.inc_volume()
print "Current volume: ", self.volume
raw_input("\n\nPress enter to exit.")
'get_volume'방법은 어디에 있습니까? –
@ SlavenTomac 나는 하나도 만들지 않았다. 나는 그것이 필요하다고 생각하지 않았다. – BBedit