这段代码并不是按我想要的那样打印 - python [英] this code isn't printing as I want it to - python

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

问题描述

使用此代码,我没有获得
我想要的显示。

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

def makeTime(秒):
time = Time()
print已开始将秒转换为小时和分钟
time.hours = seconds / 3600
print转换成小时和小时数为:,time.hours
秒=秒 - time.hours * 3600
print剩余秒数:,秒
time.minutes =秒/ 60
print现在分钟数是:, time.minutes
seconds = seconds - time.minutes * 60
print现在剩下的秒数是:,秒
time.seconds =秒
print现在总时间是:,printTime(time)

最后一行导致的问题是:

  print现在总时间是:,printTime(时间)

我希望结果在t中他的格式低于
总时间现在是:12:42:25

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

但是当我写这行时:

 打印总时间现在是:
printTime(time)

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



当我不写
时没有任何东西出现printTime(time)函数与print相同。



那么,这里发生了什么?

编辑:我尝试使用return语句,但结果仍然相同。所以,我应该在哪里使用return语句。也许我错误地使用了它。
i试图做为


  print现在总时间为:,返回printTime(时间)

但是这样会出错。

这就是 -

  print现在总时间是:,printTime(时间)
返回printTime(时间)

仍然会得到相同的结果。 >解决方案

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

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



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

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

然后我们e

  print现在总时间为:,formatTime(time)

str.format()方法使用 0 ,python索引是从0开始的)格式化字符串语法,并插入属性从该参数。第一个参数是您的时间实例。



您可以扩展它并添加更多格式,例如零 - 填充你的数字:

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


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)

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

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

but when I write that line as:

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

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

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?

EDIT: 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)

but this gives an error.

then I tried doing it this way -

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

still get the same result.

解决方案

You are printing the return value of the printTime() function.

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

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)

then use

print "total time now is:",formatTime(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天全站免登陆