Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7519001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:49:57+00:00 2026-05-30T01:49:57+00:00

So I’ll admit that this is a homework assignment, but I’m not asking you

  • 0

So I’ll admit that this is a homework assignment, but I’m not asking you all to do it for me, I’m just looking for some guidance. We need to make a Python program to accept a time in Hours:Minutes (2:30) format in a single string, and output the amount of time in minutes. (i.e. 2 hours and 30 minutes = 150 minutes)

I still need to work out some limitations for the string input:

  1. Make sure it only uses digits and a colon
  2. Make sure it can only accept five characters (##:##)
  3. Make sure that the middle character is a colon (i.e. numbers are in correct order)
  4. and make sure that if a time like 4:35 was entered, that a zero would be added in front automatically

I’ll work on that later — for now I decided to work on the math I would get from the input.

It made sense to me to slice the string into two parts: hours and minutes. I then multiplied the amount of hours by 60 and added them to the pre-existing minutes to get a total amount of minutes. However, right now, entering a time like 02:45 is outputting a minute amount of 02020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020245.

Any idea what might be going wrong here? Just to be clear, this is homework and I want to work out the input limitations on my own, I just need help moving past this math issue.

#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.")
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T01:49:58+00:00Added an answer on May 30, 2026 at 1:49 am

    Right now, hours and minutes are string variables, not ints. Thus, you can’t multiply them as you would a number.

    Change lines 20 and 21 to

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

    And putting in 02:45 should work. However, you will still run into problems if you don’t have that leading 0 (like if you put in 2:45), so might I suggest you instead split it around the “:”, like so:

    hours = int(time.split(":")[0])
    minutes = int(time.split(":")[1])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a French site that I want to parse, but am running into
I need to clean up various Word 'smart' characters in user input, including but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.