[Tutor] Television
Noah Hall
enalicho at gmail.com
Sat Jun 25 10:36:10 CEST 2011
On Sat, Jun 25, 2011 at 9:18 AM, Vincent Balmori
<vincentbalmori at yahoo.com> wrote:
>
> The question for this is to make a program that simulates a TV by creating it
> as an object. The user should be able to to enter a channel and or raise a
> volume. Make sure that the Channel Number and Volume stay within valid
> ranges. Before I can even start correcting my code this error shows up. I
> thought I had already created the "tv" instance.
> http://old.nabble.com/file/p31925053/Television Television
>
> Traceback (most recent call last):
> File
> "/Users/vincentbalmori/Desktop/Python/py3e_source/chapter08/Television",
> line 83, in <module>
> main()
> File
> "/Users/vincentbalmori/Desktop/Python/py3e_source/chapter08/Television",
> line 46, in main
> print(Television(tv.display))
> NameError: global name 'tv' is not defined
The problem is exactly as stated. There is no tv instance - it doesn't
exist. You can't call Television(tv.display) because there's simply
nothing called tv, and therefore it will not have a display method.
You're also calling the method incorrectly - if tv did exist, and it
had the method display, what you've done there is refer to the method,
and not call it. So, if you print it, you'd get something like
<unbound method tv.display>
>>> def a():
... pass
...
>>> something = a
>>> print something
<function a at 0x0000000001F82278>
>>>
This isn't what you want, so remember to include brackets.
Instead, try -
tv = Television()
tv.display()
Note how there's no print on the tv.display()? That's because inside
Television.display, you've already told it to print, and told it to
return nothing (therefore None, by default), for example -
>>> def b():
... print 'This is something'
...
>>> print b()
This is something
None
See how None is printed?
Also, you should use better method functions - if you had called
"vold" "volume_down" instead, there would be no need for your comment
to explain what it is, and will result in less random guessing.
HTH.
More information about the Tutor
mailing list