4

所以我承认这是一个家庭作业,但我并不是要你们所有人为我做这件事,我只是在寻找一些指导。我们需要让 Python 程序在单个字符串中接受 Hours:Minutes (2:30) 格式的时间,并以分钟为单位输出时间量。(即 2 小时 30 分钟 = 150 分钟)

我仍然需要为字符串输入解决一些限制:

  1. 确保它只使用数字和冒号
  2. 确保它只能接受五个字符 (##:##)
  3. 确保中间字符是冒号(即数字顺序正确)
  4. 并确保如果输入像 4:35 这样的时间,则会在前面自动添加一个零

我稍后会处理这个问题——现在我决定处理从输入中得到的数学。

对我来说,将字符串分成两部分是有意义的:小时和分钟。然后,我将小时数乘以 60,并将它们添加到预先存在的分钟数中以获得总分钟数。但是,现在,输入像 02:45 这样的时间会输出 020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020 的分钟数。

知道这里可能出了什么问题吗?需要明确的是,这是作业,我想自己解决输入限制,我只需要帮助解决这个数学问题。

#Henry Quinn - Python Advanced 4.0 Hours and Minutes
import re
print "This program takes an input of time in hours and minutes and outputs the amount    of minutes."
count = 0

#I still need to work out while loop
#Supposed to make sure that a time is entered correctly, or error out
while (count <1):
    time = raw_input("Please enter the duration of time (ex: 2:15 or 12:30): ")
    if not re.match("^[0-9, :]*$", time):
        print "Sorry, you're only allowed to use the numbers 0-9."
    elif len(time) > 5:
        print "Sorry, only five characters max allowed."
#MAKE THIS CHECK FOR A COLON
#elif
#elif
    else:
        count = count + 1

#If time = 12:45, hours should be equal to 12, and minutes should be equal to 45
hours = time[:2]
minutes = time[3:]

#Should convert hours to minutes
newhours = hours * 60

#Should make total amount of minutes
totalminutes = newhours + minutes

print "The total amount of elapsed minutes is %s" % (totalminutes)

raw_input("Please press Enter to terminate the program.")
4

4 回答 4

5

现在,小时和分钟是字符串变量,而不是整数。因此,您不能像数字一样将它们相乘。

将第 20 和 21 行更改为

hours = int(time[:2])
minutes = int(time[3:])

输入 02:45 应该可以。但是,如果您没有前导 0(例如输入 2:45),您仍然会遇到问题,所以我建议您改为将其拆分为“:”,如下所示:

hours = int(time.split(":")[0])
minutes = int(time.split(":")[1])
于 2012-02-13T18:03:14.273 回答
3

您正在将字符串与整数相乘。

>>> st = '20'
>>> st*3
'202020'
>>> int(st)*3
60
>>>

类型将其转换为int.

所以,改变这个

minutes = time[3:]
newhours = hours * 60

 minutes = int(time[3:])
 newhours = int(hours) * 60
于 2012-02-13T18:03:46.177 回答
1

因为这是家庭作业,所以这里有一个解决方案——如果你弄清楚它是如何工作的,我保证你会学到一些新东西;)

tre = re.compile("([0-2]?[0-9]):([0-5][0-9])")
h,m = ((int(_) for _ in tre.match("2:30").groups())
td = timedelta(hours=h, minutes=m)
print(td.total_seconds() / 60)
于 2012-02-13T18:14:50.923 回答
1

第 2 项和第 4 项要求相互矛盾。要么只接受 5 个字符串,要么也允许#:##(4 个字符形式)。

import re

def minutes(timestr):
    """Return number of minutes in timestr that must be either ##:## or #:##."""
    m = re.match(r"(\d?\d):(\d\d)$", timestr)
    if m is None:
       raise ValueError("Invalid timestr: %r" % (timestr,))
    h, m = map(int, m.groups())
    return 60*h + m

如果您在timestrand ##:#, #:#, etc 表单中允许空格,则:

def minutes2(timestr):
    h, m = map(int, timestr.partition(':')[::2])
    return 60*h + m

如果您想将小时数限制为 0..23,分钟数限制为 0..59,则:

import time

def minutes3(timestr):
    t = time.strptime(timestr, "%H:%M")
    return 60*t.tm_hour + t.tm_min

例子

minutes ('12:11') -> 731
minutes2('12:11') -> 731
minutes3('12:11') -> 731
minutes ('  12:11') -> error: Invalid timestr: '  12:11'
minutes2('  12:11') -> 731
minutes3('  12:11') -> error: time data '  12:11' does not match format '%H:%M'
minutes ('12:11  ') -> error: Invalid timestr: '12:11  '
minutes2('12:11  ') -> 731
minutes3('12:11  ') -> error: unconverted data remains:   
minutes ('3:45') -> 225
minutes2('3:45') -> 225
minutes3('3:45') -> 225
minutes ('03:45') -> 225
minutes2('03:45') -> 225
minutes3('03:45') -> 225
minutes ('13:4') -> error: Invalid timestr: '13:4'
minutes2('13:4') -> 784
minutes3('13:4') -> 784
minutes ('13:04') -> 784
minutes2('13:04') -> 784
minutes3('13:04') -> 784
minutes ('24:00') -> 1440
minutes2('24:00') -> 1440
minutes3('24:00') -> error: time data '24:00' does not match format '%H:%M'
minutes ('11:60') -> 720
minutes2('11:60') -> 720
minutes3('11:60') -> error: unconverted data remains: 0
于 2012-02-13T18:43:39.523 回答