(Python)使用取模将余数从秒数更改为小时和分钟 [英] (Python) Using modulo to get the remainder from changing secs, to hrs and minutes

查看:377
本文介绍了(Python)使用取模将余数从秒数更改为小时和分钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前刚开始学习python,偶然发现了这个问题:

I'm currently new to learning python and stumbled upon this problem:

练习2-7抽出时间 编写算法,以秒为单位读取时间量,然后显示等效时间 小时,分钟和剩余的秒数. •一小时对应60分钟. •一分钟对应60秒.

Exercise 2-7 Get the Time Write an algorithm that reads the amount of time in seconds and then displays the equivalent hours, minutes and remaining seconds. • One hour corresponds to 60 minutes. • One minute corresponds to 60 seconds.

这是我的写法:

def amount_time(t):
  h = t//3600
  m = int((t/3600 - h)*60)
  s = int(((t/3600 - h)*60 - m)*60)
  print("Number of hours:",h)
  print("Number of minutes:",m)
  print("Number of Seconds:", s)
amount_time(5000)

有人告诉我,有一种更简单的方法可以使用模写来获取余下的分钟和几秒钟,有人可以帮忙吗?谢谢!

I was told there was an easier way to write it using modulo to get the remainder for the minutes and seconds, could someone help? Thanks!

推荐答案

这只是烦恼",因为我现在没有可以使用的测试系统.

This is just "out of my head", because I got no testing system I could use right now.

def amount_time(t):
    print("Number of Seconds:", t % 60)
    print("Number of Minutes:", (t // 60) % 60)
    print("Number of Hours:", (t // 3600))

"t%60"的作用是什么:

What "t % 60" does:

  1. 取t,除以60
  2. 删除圆点左侧的所有内容
  3. 乘以60

带数字:

  1. 5000/60 = 83.33333
  2. => 0.33333
  3. 0.33333 * 60 = 20

这篇关于(Python)使用取模将余数从秒数更改为小时和分钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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