Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
这段代码没有按我所希望的那样打印-python_Python_Function_Printing - Fatal编程技术网

这段代码没有按我所希望的那样打印-python

这段代码没有按我所希望的那样打印-python,python,function,printing,Python,Function,Printing,使用此代码,我不会 我想要的那种展示 def printTime(time): print time.hours,":",time.minutes,":",time.seconds, def makeTime(seconds): time = Time() print "have started converting seconds into hours and minutes" time.hours = seconds/3600 print "conv

使用此代码,我不会 我想要的那种展示

def printTime(time):
    print time.hours,":",time.minutes,":",time.seconds,

def makeTime(seconds):
    time = Time()
    print "have started converting seconds into hours and minutes"
    time.hours = seconds/3600
    print "converted into hours and no.of hours is :",time.hours
    seconds = seconds - time.hours *3600
    print "number of seconds left now:",seconds
    time.minutes = seconds/60
    print "number of minutes now is :",time.minutes
    seconds = seconds - time.minutes*60
    print "number of seconds left now is :",seconds
    time.seconds = seconds
    print "total time now is:",printTime(time)
最后一行导致的问题是:

print "total time now is:",printTime(time)
我希望结果的格式如下- 现在的总时间是:12:42:25

但我得到的是 现在的总时间是:12:42:25无

但当我把这句话写成:

print "total time now is:"
printTime(time)
然后我得到的结果是- 现在的总时间是: 12:42:25

当我不写的时候,没有东西出现 printTimetime函数与print位于同一行

那么,这里到底发生了什么

编辑:我尝试使用return语句,但结果还是一样。那么,我应该在哪里使用return语句呢。也许我用错了。 我试着像你一样去做

print "total time now is:",return printTime(time)
但这是一个错误

然后我试着这样做-

print "total time now is:",printTime(time)
return printTime(time)
仍然会得到相同的结果。

您正在打印printTime函数的返回值

Python中的所有函数都有一个返回值,如果不使用return语句,该值默认为None

不要在printTime函数中打印,而是将该函数重命名为formatTime并使其返回格式化字符串:

def formatTime(time):
    return '{0.hours}:{0.minutes}:{0.seconds}'.format(time)
然后使用

print "total time now is:",formatTime(time)
上面使用的是引用0中传递的第一个参数的,python索引是基于0的,并从该参数中插入属性。第一个参数是您的时间实例

您可以在此基础上展开并添加更多格式,例如数字的零填充:

def formatTime(time):
    return '{0.hours:02d}:{0.minutes:02d}:{0.seconds:02d}'.format(time)
您正在打印printTime函数的返回值

Python中的所有函数都有一个返回值,如果不使用return语句,该值默认为None

不要在printTime函数中打印,而是将该函数重命名为formatTime并使其返回格式化字符串:

def formatTime(time):
    return '{0.hours}:{0.minutes}:{0.seconds}'.format(time)
然后使用

print "total time now is:",formatTime(time)
上面使用的是引用0中传递的第一个参数的,python索引是基于0的,并从该参数中插入属性。第一个参数是您的时间实例

您可以在此基础上展开并添加更多格式,例如数字的零填充:

def formatTime(time):
    return '{0.hours:02d}:{0.minutes:02d}:{0.seconds:02d}'.format(time)
printTime返回一个打印函数调用,然后尝试打印

将打印时间更改为:

或者,更有效地:

return "%s:%s:%s" % (time.hours, time.minutes, time.seconds)
printTime返回一个打印函数调用,然后尝试打印

将打印时间更改为:

或者,更有效地:

return "%s:%s:%s" % (time.hours, time.minutes, time.seconds)

Chad,返回time.hours+:+time.minutes+:+time.seconds行出现类型错误:不支持的操作数类型。第二行很好用。现在,我不明白为什么会发生这种情况?@faraz,哦,那是因为time.hours、time.minutes和time.seconds返回整数或一些非字符串值,python无法将字符串转换为非字符串。因此,真正的编写方法是返回strtime.hours+:+strtime.minutes等,但由于python处理字符串连接的方式,效率要低得多,因此我建议使用第二行。Chad,return time.hours+:+time.minutes+:+time.seconds行给出了一个类型错误:不支持的操作数类型。第二行很好用。现在,我不明白为什么会发生这种情况?@faraz,哦,那是因为time.hours、time.minutes和time.seconds返回整数或一些非字符串值,python无法将字符串转换为非字符串。因此,真正的编写方法是返回strtime.hours+:+strtime.minutes等。但是由于python处理字符串连接的方式,效率要低得多,所以我建议使用第二行。嗨,Martijn,你的代码工作得很好,但是我不理解这里“{0.hours}:{0.minutes}:{0.seconds}”的用法。你能解释一下吗?0代表什么?我用time.minutes和time.hours来代替。@faraz:我把答案扩大了一点;您还可以使用关键字参数:{time.hours}:{time.minutes}:{time.seconds}.formattime=time。感谢您的解释。我还将阅读有关格式字符串语法的内容。您好,Martijn,您的代码工作得很好,但我不理解这里“{0.hours}:{0.minutes}:{0.seconds}”的用法。你能解释一下吗?0代表什么?我用time.minutes和time.hours来代替。@faraz:我把答案扩大了一点;您还可以使用关键字参数:{time.hours}:{time.minutes}:{time.seconds}.formattime=time。感谢您的解释。我还将阅读有关格式字符串语法的内容。