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

时间:2018-08-09 09:51:13

标签: python string class object printing

我正在尝试创建一个计数器,该计数器将显示程序启动后所经过的时间。我是OOP的新手,所以我决定从中学习。我在打印不带括号和引号的输出时遇到问题

代码如下:

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 个答案:

答案 0 :(得分:0)

您的属性有多种格式。它们被初始化为字符串,然后取决于各种条件,可能会重新分配给(int,string)元组。也许你的意思是例如

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

我怀疑混淆来自打印如何将看起来像元组的字符串变成串联的字符串...

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

但是它只是打印可变数量的参数,在一行上用空格串联起来-那里没有元组。相反,打印一个元组可以得到...

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

您想要的地方

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

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

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