代码之家  ›  专栏  ›  技术社区  ›  Viqtoh

无法从没有圆括号和引号的类中打印整数和字符串

  •  -1
  • Viqtoh  · 技术社区  · 6 年前

    我正在尝试创建一个计数器,它将显示程序启动后经过的时间。我是新手,所以我决定学这个。没有括号和引号的输出打印有问题

    代码如下:

    from platform import system as system_name
    from os import system as system_call
    from time import sleep
    class Countr(object):
      def __init__(self,second,minute,hour,day,week,month,year,decade,century,):
        self.second = second
        self.minute = minute
        self.hour =  hour
        self.day = day
        self.week = week
        self.month = month
        self.year = year
        self.decade = decade
        self.century = century
        self.ce="0 centuries"
        self.de="0 decades"
        self.y="0 yrs"
        self.mo="0 mths"
        self.w="0 wks"
        self.d="0 days"
        self.h="0 hrs"
        self.m="0 mins"
    
    
    
     def outpt(self):
        if (self.second==1):
            self.s=(self.second , "sec")
        else:
            self.s=(self.second , "secs")
        if (self.second==60):
            self.minute +=1
            self.second = 0
            if (self.minute==1):
                self.m=(self.minute , "min ")
            else:
                self.m=(self.minute , "mins ")
        elif (self.minute==60):
            self.hour +=1
            self.minute = 0
            if (self.hour==1):
                self.h=(self.hour , "hr ")
            else:
                self.h=(self.hour , "hrs ")
        elif (self.hour==24):
            self.day +=1
            self.minute = 0
            if (self.day==1):
                self.d=(self.day , "day ")
            else:
                self.d=(self.day , "days ")
        elif (self.day==7):
            self.week +=1
            self.day = 0
            if (self.week==1):
                self.w=(self.week , "wk ")
            else:
                self.w=(self.week , "wks ")
        elif (self.week==4):
            self.month +=1
            self.week = 0
            if (self.month==1):
                self.mo=(self.month , "mth ")
            else:
                self.m=(self.month , "mths ")
        elif (self.month==12):
            self.year +=1
            self.month = 0
            if (self.year==1):
                self.y=(self.year , "yr ")
            else:
                self.y=(self.year , "yrs ")
        elif (self.year==10):
            self.decade +=1
            self.year = 0
            if (self.decade==1):
                self.de=(self.decade , "decade ")
            else:
                self.de=(self.decade , "decades ")
        elif (self.decade==10):
            self.century +=1
            self.decade = 0
            if (self.century==1):
                self.ce=(self.century , "century ")
            else:
                self.ce=(self.century , "centuries ")
        print ((self.ce),(self.de),(self.y),(self.mo),(self.w),(self.d),(self.h),(self.m),(self.s))
    
    initial= Countr(0,0,0,0,0,0,0,0,0)
    while 1:
        sleep(1)
        command = "cls"
        system_call(command)
        initial.second += 1
        initial.outpt()
    

    一切都很好,除了像这样的指纹:

    0 centuries 0 decades 0 yrs 0 mths 0wks 0 days 0 hrs (14, 'mins ') (6, 'secs')
    

    拜托。。。我需要你的帮助!

    1 回复  |  直到 6 年前
        1
  •  0
  •   joel DeyaEldeen    6 年前

    你的属性有多种格式。它们被初始化为字符串,然后根据不同的条件,可能被重新分配到(int,string)元组。也许你的意思是。

    self.h = str(self.hour) + "hr "
    

    我怀疑这种混乱来自print如何将看起来像元组的内容转换成连接字符串。。。

    >>> print(1, "cat")
    1 cat
    

    但它只打印了一行可变数量的参数,用空格连接起来,没有元组。相反,打印元组会。。。

    >>> a = (1, "cat")
    >>> print(a)
    (1, 'cat')
    

    在你想要的地方

    >>> a = str(1) + " cat"
    >>> print(a)
    1 cat
    

    也就是说,不能通过使用逗号分隔的值复制print函数调用语法来创建字符串

    注意:当你 print(...) ,所有内括号都是多余的,因为它们只包含一个对象