Найти - Пользователи
Полная версия: Задача по Python, ошибка "TypeError: 'int' object is not callable"
Начало » Python для новичков » Задача по Python, ошибка "TypeError: 'int' object is not callable"
1
Bart07
Начинаю изучать Python по книге М.Доусона. Задача звучит так:
“Создайте программу, имитирующую телевизор как объект. У пользователя должна быть возможность вводить номер канала, а также увеличивать и уменьшать громкость. Программа допжна следить за тем, чтобы номер канала и уровень громкости оставались в допустимых пределах.”
 class TV(object):
    def __init__(self, chanel = 1, volume = 5):
        self.volume = volume
        self.chanel = chanel
    def chanels(self, choice):
        if choice == "1":
            print("News on screen! Today is a great day!")
        elif choice == "2":
            print("Horror movie on screen... Be careful and stay alive")
        elif choice == "3":
            print("Wheee! Cartooooons!")
        self.chanel = choice
    def volume(self, choice):
        if choice == "+":
            self.volume += 1
            if self.volume > 10:
                self.volume = 10
        elif choice == "-":
            self.volume -= 1
            if self.volume < 0:
                self.volume = 0
        print("Now volume is", self.volume)
def main():
    tv = TV()
    chanel_list = ["1", "2", "3"]
    choice = None
    while choice != "0":
        print("""
               0 - exit
              (1-3) - chanels
               +/- to change volume
               """)
        choice = input("Choice: ")
        if choice == "0":
            print("Have a nice day!") 
        elif choice in chanel_list:
            tv.chanels(choice)
        elif choice == "+" or "-":
            tv.volume(choice)
        else:
            print(choice, "is invalid choice.")
Собственно, при вызове tv.volume(choice) вылезает ошибка TypeError: ‘int’ object is not callable. Интуитивно понимаю, что решение очевидно, но бьюсь над этим уже долго… Помогите, пожалуйста.
Stright
У вас в классе метод и атрибут называются одинаково, и интерпритатор пытается вызвать атрибут self.volume, который равен целому числу
Bart07
Stright
Спасибо!
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB