0

Hi,

The following code (BLOCK 1) is intended to simulate a television by creating it as an object, and is it stands I am pretty happy with it. Having created a volume control however, I then wanted to add a brightness and contrast control. My first attempt at doing this involved adding a contrast_adjust() method, and a brightness_adjust() method. This entailed copying the volume_adjust() method and substituting the word "volume" with "contrast" or "brightness" wherever needed. As you can imagine however this led to a lot of duplication, so I set about creating a single adjust() method which could be used to adjust all 3 controls (see BLOCK 3 for how I tried to do this). Unfortunately this doesn't work - python won't let me pass a value (e.g. "volume") to the adjust method and then let this value be used as the PROPERTY attribute (if you see what I mean). Can anyone see a way around this?

CODE BLOCK 1.

# Creating a television
class Television(object):
	"""A virtual televsion"""
	def __init__(self, volume = 20, channel = 1):
		self.volume = volume
		self.channel = channel
	
	def change_channel(self, number):
		if number > 5:
			print "Channel not available"
		else:	
			self.channel = number
			print "You are now watching channel", self.channel
	
	def volume_adjust(self):
		print "-\tVolume (", self.volume, ")\t   +"
		adjust = raw_input("")
		while adjust == "+" or adjust == "-":

			if adjust == "+":
				self.volume += 1
			else:
				self.volume -= 1
			
			if self.volume < 0:
				self.volume = 0
			elif self.volume > 20:
				self.volume = 20
			else:
				pass

			print "-\tVolume (", self.volume, ")\t   +"
			adjust = raw_input("")
		



def remote():

	print """
		  _____________
		|                 |
		|    1 2 3     |
		|    4 5 6     |
		|    7 8 9     |
		|                |
		| |V|olume     |
		| |C|ontrast    |
		| |B|rightness |
		|                    |
		|    +   -          |
		|              |
		 --------------
		"""



def main():
	
	tele = Television()
	remote()	
	remote_control = raw_input("Make your choice: ")
	
	while remote_control != "x":
	
		try:
			if int(remote_control) in range(1, 101):
				tele.change_channel(int(remote_control))
	
		except(ValueError):
			if (remote_control) == "v":
				tele.volume_adjust()
			else:
				pass
		
		remote()
		remote_control = raw_input("Make your choice: ")

	print "Television now on standby."

main()

CODE BLOCK 2:


CODE BLOCK 3:

class Television(object):
	"""A virtual televsion"""
	def __init__(self, volume = 20, brightness = 20, contrast = 20, channel = 1):
		self.volume = volume
		self.channel = channel
		self.brightness = brightness
		self.contrast = contrast
	
	def change_channel(self, number):
		if number > 5:
			print "Channel not available"
		else:	
			self.channel = number
			print "You are now watching channel", self.channel
	
	def vol(self):
		print "-\tVolume (", self.volume, ")\t   +"
		self.adjust("volume")
	
	def bright(self):
		print "-\tBrightness (", self.brightness, ")\t   +"
		self.adjust("brightness")
	
	def contr(self):
		print "-\tContrast (", self.contrast, ")\t   +"
		self.adjust("Contrast")		

	
	def adjust(self, PROPERTY):
		print self.PROPERTY
		adjust = raw_input("")
		while adjust == "+" or adjust == "-":

			if adjust == "+":
				self.PROPERTY += 1
			else:
				self.PROPERTY -= 1
			
			if self.PROPERTY < 0:
				self.PROPERTY = 0
			elif self.PROPERTY > 20:
				self.PROPERTY = 20
			else:
				pass

			print "-\t", PROPERTY, self.PROPERTY, ")\t   +"
			adjust = raw_input("")
2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by Mathhax0r
0

How about having a dictionary containing volume, brightness, and contrast?

def adjust(self, PROPERTY):
		print self.stats[PROPERTY]
		adjust = raw_input("")
		while adjust == "+" or adjust == "-":
 
			if adjust == "+":
				self.stats[PROPERTY] += 1
			else:
				self.stats[PROPERTY] -= 1
 
			if self.stats[PROPERTY] < 0:
				self.stats[PROPERTY] = 0
			elif self.stats[PROPERTY] > 20:
				self.stats[PROPERTY] = 20
			else:
				pass
 
			print "-\t", PROPERTY, self.stats[PROPERTY], ")\t   +"
			adjust = raw_input("")

I'm pretty sure you'll get it from here. You can also as as many other statuses to the TV as you want this way, and have almost no extra code.

Edited by Mathhax0r: Forgot to include a bit of code.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.