【问题标题】:Can't print integer and string from class without parentheses and quotation marks无法从没有括号和引号的类中打印整数和字符串
【发布时间】:2018-08-09 09:51:12
【问题描述】:

我正在尝试创建一个计数器,以显示从程序启动后经过的时间。我是 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')

请...我需要你的帮助!

【问题讨论】:

    标签: python string class object printing


    【解决方案1】:

    您的属性有多种格式。它们被初始化为字符串,然后根据各种条件,可能会重新分配给 (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(...)时,所有内括号都是多余的,因为它们只包含一个对象

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 2017-09-24
      • 1970-01-01
      • 2021-09-01
      • 2017-02-25
      相关资源
      最近更新 更多