0

The closest thread I could find to my problem was this: Python setter does not change variable

It didn't really help, the volume and the channels are not changing at all. The first fucntion which is to "watch TV" works perfectly fine.

class TV(object):

    def __init__(self, channel, volume):
        self.__channel = channel
        self.__volume = volume

    def __str__(self):
        out = ""
        out += "You're on channel #" + str(self.__channel) + ", " +     self.channelNetwork()
    out += "\nVolume is currently at: " + str(self.__volume) + "/20"
    return out

# a method that determines the name for each channel from 1-10

def channelNetwork(self):
    c = self.__channel
    if c == 1:
        return "CBS"
    elif c==2:
        return "NBC"
    elif c==3:
        return "ABC"
    elif c==4:
        return "Fox"
    elif c==5:
        return "ESPN"
    elif c==6:
        return "PBS"
    elif c==7:
        return "CNN"
    elif c==8:
        return "Comedy Central"
    elif c==9:
        return "Cartoon Network"
    elif c==10:
        return "Nicklodeon"

# a volume slider that has a range from 0-20

def volumeSlider(self, newVolume):
    v = self.__volume
    if newVolume == "+":
        v += 1
    else:
        v -= 1
    if v < 0:
        v = 0
    if v > 20:
        v = 20

def channelChanger(self, newChannel):
    c = self.__channel
    if newChannel == "+":
        c += 1
    else:
        c -= 1
    if c < 0:
        c = 0
    if c > 10:
        c = 10

def main():
import random
randomChannel = random.randint(1,10)
randomVolume = random.randrange(21)
televsion = TV(randomChannel, randomVolume)

choice = None
while choice != "0":
    print \
    ("""
    TV Simulator

    0 - Turn off TV
    1 - Watch TV
    2 - Change channel
    3 - Change volume
    """)

    choice = input("Choice: ")
    print()

    # exit
    if choice == "0":
        print("Have a good day! :)")

    elif choice == "1":
        print("You relax on your couch and watch some TV")
        print(televsion)

    elif choice == "2":
        newChannel = None
        while newChannel not in ('+', '-'):
            newChannel = input("\nPress '+' to go up a channel and press '-' to go down a channel: ")
        televsion.channelChanger(newChannel)

    elif choice == "3":
        newVolume = None
        while newVolume not in ('+', '-'):
            newVolume = input("\nPress '+' to increase volume and press '-' to decrease volume: ")
        televsion.volumeSlider(newVolume)

    else:
        print("\nSorry, but", choice, "isn't a valid choice.")

main()
input("\n\nPress enter to exit.")
2
  • Why are you using double-underscores for your instance variables? Commented Apr 8, 2017 at 21:10
  • 1
    You never change the values of self.__channel or self.__volume inside your methods. You set other variables equal to the values of those variables, but then reassign those variables to other values, but the originals remain unchanged. Commented Apr 8, 2017 at 21:10

1 Answer 1

3

The problem is, that when you do:

v = self.__volume

In:

def volumeSlider(self, newVolume):
    v = self.__volume
    if newVolume == "+":
        v += 1
    else:
        v -= 1
    if v < 0:
        v = 0
    if v > 20:
        v = 20

Assigning to v won't affect self.__volume. You need to use self.__volume = 20 or whatever.

As an aside, don't use double-underscore name-mangling unless you actually need it. E.g. self.volume is fine.

Sign up to request clarification or add additional context in comments.

5 Comments

Oh, I see, so v just makes a "shallow copy" or whatever it's called! Makes sense, I guess my desire to make my code look more clean really screwed me on this one!
@AlexRosenbach NO. It doesn't make any sort of copy at all. Please read the following by StackOverflow legend: nedbatchelder.com/text/names.html
@AlexRosenbach Also, this is a good summary as well. foobarnbaz.com/2012/07/08/understanding-python-variables The idea is to think of variables, which should probably be called "names", like name tags.
Wow, that's definitely an insta-bookmark! Thanks for clearing that up!
@AlexRosenbach Assignment in Python never makes a copy. Try to keep that in mind, and a lot of stuff that may be confusing starts to make sense.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.