此代码不会按我希望的那样打印-python [英] this code isn't printing as I want it to - python

查看:46
本文介绍了此代码不会按我希望的那样打印-python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用此代码,我没有得到我想要的显示类型.

With this code,I am not getting the kind of display I want.

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)

最后一行引起的问题是:

The last line is causing the problem that is:

print "total time now is:",printTime(time)

我希望结果采用以下格式-现在总时间是:12:42:25

I want the result to be in the format below- total time now is : 12:42:25

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

but what i get is total time now is : 12:42:25 None

但是当我将该行写为:

print "total time now is:"
printTime(time)

然后我得到的结果是-现在的总时间是:12:42:25

then i get the result as - total time now is : 12:42:25

当我不写的时候,没有东西没有出现printTime(time)函数与print在同一行.

The None thing doesn't appear when I dont write the printTime(time) function in the same line as print.

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

So,what's really happening here?

我尝试使用return语句,但是结果仍然相同.所以,我到底应该在哪里使用return语句.也许我没有正确使用它.我尝试这样做

I tried using the return statement, but the result was still the same. so,where exactly should I use the return statement. maybe i was using it incorrectly . i tried doing it as

print "total time now is:",return printTime(time)

但这会导致错误.

然后我尝试用这种方式-

then I tried doing it this way -

print "total time now is:",printTime(time)
return printTime(time)

仍然得到相同的结果.

推荐答案

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

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

All functions in Python have a return value, and if you don't use a return statement, that value defaults to None.

而不是在 printTime()函数中进行打印,而是将该函数重命名为 formatTime(),并使它返回格式化的字符串:

Instead of printing in the printTime() function, rename that function to formatTime() and have it return the formatted string:

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

然后使用

print "total time now is:",formatTime(time)

str.format()方法上面使用的格式字符串语法指的是第一个传入的参数( 0 ,python索引基于0),并从该参数插入属性.第一个参数是您的 time 实例.

The str.format() method above uses a format string syntax that refers to the first parameter passed in (0, python indexing is 0-based), and interpolates attributes from that parameter. The first parameter is your time instance.

您可以对此进行扩展并添加更多格式,例如将数字零填充:

You can expand on that and add some more formatting, such as zero-padding your numbers:

def formatTime(time):
    return '{0.hours:02d}:{0.minutes:02d}:{0.seconds:02d}'.format(time)

这篇关于此代码不会按我希望的那样打印-python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆