So, I admit that this is homework, but I do not ask you all to do it for me, I'm just looking for guidance. We need to make a Python program to accept the time in the format Hours: Minutes (2:30) on one line and print the time in minutes. (i.e. 2 hours and 30 minutes = 150 minutes).
I still need to work out some restrictions for entering a string:
- Be sure to use only numbers and a colon
- Make sure it can only accept five characters (##: ##)
- Make sure the middle character is a colon (i.e. the numbers are in the correct order)
- and make sure that if a time such as 4:35 was entered, zero will be added automatically automatically
I will work on this later - for now, I decided to work on the math that I would get from input.
It was wise for me to cut the string into two parts: hours and minutes. Then I multiplied the number of hours by 60 and added them to the pre-existing minutes to get the total number of minutes. However, right now, during, for example, 02:45, a minute quantity of 0202020202020202020202020202020202020202020202020202020202020202020202020202020205 is issued.
Any idea what might be wrong here? To be clear, this is homework, and I want to deal with input restrictions myself, I just need help in promoting this mathematical problem.
#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.")
source share