小编nos*_*klo的帖子

我正在制作模拟电视

我需要制作一个向用户显示频道和音量的电视,并显示电视是否打开.我有大部分代码,但由于某种原因,频道不会切换.我对属性的工作方式还不太熟悉,我认为这就是我的问题所在.请帮忙.

class Television(object):

    def __init__(self, __channel=1, volume=1, is_on=0):
        self.__channel=__channel
        self.volume=volume
        self.is_on=is_on

    def __str__(self):
        if self.is_on==1:
            print "The tv is on"
            print self.__channel
            print self.volume
        else:
            print "The television is off."

    def toggle_power(self):
        if self.is_on==1:
            self.is_on=0
            return self.is_on
        if self.is_on==0:
            self.is_on=1
            return self.is_on

    def get_channel(self):
        return channel

    def set_channel(self, choice):
        if self.is_on==1:
            if choice>=0 and choice<=499:
                channel=self.__channel
            else:
                print "Invalid channel!"
        else:
            print "The television isn't on!"

    channel=property(get_channel, set_channel)

    def raise_volume(self, up=1):
        if self.is_on==1:
            self.volume+=up
            if self.volume>=10:
                self.volume=10
                print "Max volume!" …
Run Code Online (Sandbox Code Playgroud)

python properties class

1
推荐指数
1
解决办法
2260
查看次数

无法理解Python中输出的原因

我刚开始学习Python.我遇到了以下代码

if 0:

  print "And now for something completely different..."
Run Code Online (Sandbox Code Playgroud)

else:

  print "What's all this, then?"
Run Code Online (Sandbox Code Playgroud)

我知道输出应该是,What's all this, then?但我似乎无法找到适当的解释.希望有人能让我理解.

谢谢.

python

1
推荐指数
1
解决办法
98
查看次数

时间数据'2018-06-19 11:21:13.311'格式不匹配

使用python,我试图读取日期/时间格式ergo 2018-06-19 11:21:13.311,它返回一个错误:

ValueError: time data '2018-06-19 11:21:13.311' does not match format
'%Y-%m-%d %I:%M:%S.%%'
Run Code Online (Sandbox Code Playgroud)

我尝试删除.%%,但程序返回未转换的数据错误消息.有谁知道.311是什么?我怎么读它?

python time

1
推荐指数
1
解决办法
3644
查看次数

如何设置它以便线程通信他们完成任务?

从概念上讲,我想完成以下操作,但却无法理解如何在Python中正确编码:

from threading import Thread
for i in range(0,3):
    t = Thread(target=myfunction)
    t.start()

# wait until threads have finished executing
print 'complete!'
Run Code Online (Sandbox Code Playgroud)

python multithreading

0
推荐指数
1
解决办法
200
查看次数

标签 统计

python ×4

class ×1

multithreading ×1

properties ×1

time ×1