Question

In: Computer Science

Write a Python program that reads in two times, an earlier time and a later time,...

Write a Python program that reads in two times, an earlier time and a later time, and prints the difference between the two times in minutes as well as in hours/minutes. The format of the time is HH:MMAM or H:MMAM or HH:MMPM or H:MMPM. Also the AM or PM may be in lower case. A sample run of the program is shown below:

$ python3 Time.py

Enter Earlier Time: 9:36aM
Enter Later   Time: 6:22PM

Number of minutes between 9:36aM and 6:22PM = 526
The time difference between 9:36aM and 6:22PM = 8 hours and 46 minutes.

The formatting of the output should be exactly as shown above. This program should be placed in a file called Time.py.

Solutions

Expert Solution

t1=input('Enter Earlier Time: ')
t2=input('Enter Later   Time: ')

h1,m1=t1.split(':')
h2,m2=t2.split(':')

#these 2 variables will store both time as minutes starting from 00:00 am
earlierMinutes=0
laterMinutes=0

if m1[-2:].lower()=='am':
    earlierMinutes+=int(h1)*60+int(m1[:2])
else:
    if int(h1)!=12:
        earlierMinutes+=int(h1)*60+int(m1[:2])+12*60
    else:
        earlierMinutes=12*60+int(m1[:2])

if m2[-2:].lower()=='am':
    laterMinutes+=int(h2)*60+int(m2[:2])
else:
    if int(h2)!=12:
        laterMinutes+=int(h2)*60+int(m2[:2])+12*60
    else:
        laterMinutes=12*60+int(m2[:2])

difference=laterMinutes-earlierMinutes
if difference<0:
    difference=1440+difference
hrs=difference//60
mins=difference%60

print()
print('Number of minutes between',t1,'and',t2,'=',difference)
print('The time difference between',t1,'and',t2,'=',hrs,'hours and',mins,'minutes.')


Related Solutions

Python program: Write a program that reads a text file named test_scores.txt to read the name...
Python program: Write a program that reads a text file named test_scores.txt to read the name of the student and his/her scores for 3 tests. The program should display class average for first test (average of scores of test 1) and average (average of 3 tests) for each student. Expected Output: ['John', '25', '26', '27'] ['Michael', '24', '28', '29'] ['Adelle', '23', '24', '20'] [['John', '25', '26', '27'], ['Michael', '24', '28', '29'], ['Adelle', '23', '24', '20']] Class average for test 1...
write a python program that reads a sentence and identifies a word from an existing glossary...
write a python program that reads a sentence and identifies a word from an existing glossary and states the meaning of the word
Design and write a python program that reads a file of text and stores each unique...
Design and write a python program that reads a file of text and stores each unique word in some node of binary search tree while maintaining a count of the number appearance of that word. The word is stored only one time; if it appears more than once, the count is increased. The program then prints out 1) the number of distinct words stored un the tree, Function name: nword 2) the longest word in the input, function name: longest...
Using Python. Write a program that reads a sequence (unknown number of inputs) of integer inputs...
Using Python. Write a program that reads a sequence (unknown number of inputs) of integer inputs and prints the number of even and odd inputs in the sequence. please explain. Thanks
python Write a program that prints your name 100 times to the screen. Write a function...
python Write a program that prints your name 100 times to the screen. Write a function that takes a string s and an integer n as parameters and prints the string s a total of n times (once per line). Write a for loop that prints all the integers from 3141 to 5926, skipping every other one. Write a for loop that prints every 5th integer from 5926 down to 3141. Write a program (using a for loop) to print...
Python 8.17 LAB: Strings of a Frequency Write a program that reads whitespace delimited strings (words)...
Python 8.17 LAB: Strings of a Frequency Write a program that reads whitespace delimited strings (words) and an integer (freq). Then, the program outputs the strings from words that have a frequency equal to freq in a case insensitive manner. Your specific task is to write a function wordsOfFreqency(words, freq), which will return a list of strings (in the order in which they appear in the original list) that have a frequency same as the function parameter freq. The parameter...
Use PYTHON --Nested Lists and DateTime Module. Write a program that does the following: Reads information...
Use PYTHON --Nested Lists and DateTime Module. Write a program that does the following: Reads information from a text file into a list of sublists. Be sure to ask the user to enter the file name and end the program if the file doesn’t exist. Text file format will be as shown, where each item is separated by a comma and a space: ID, firstName, lastName, birthDate, hireDate, salary Store the information into a list of sublists called empRoster. EmpRoster...
Write a Python program that reads a file, input by the user, containing one word/token per...
Write a Python program that reads a file, input by the user, containing one word/token per line with an empty line between sentences. The program prints out the longest word found in the file along with its length.
Write a program IN PYTHON of the JUPYTER NOOTBOOK Write a Python program that gets a...
Write a program IN PYTHON of the JUPYTER NOOTBOOK Write a Python program that gets a numeric grade (on a scale of 0-100) from the user and convert it to a letter grade based on the following table. A: 90% - 100% B 80% - 89% C 70% - 79% D 60% - 69% F <60% The program should be written so that if the user entered either a non-numeric input or a numeric input out of the 0-100 range,...
Write a program that performs the following two tasks in java Reads an arithmetic expression in...
Write a program that performs the following two tasks in java Reads an arithmetic expression in an infix form, stores it in a queue (infix queue) and converts it to a postfix form (saved in a postfix queue). Evaluates the postfix expression. Use linked lists to implement the Queue and Stack ADTs. DO NOT USE BUILT IN JAVA CLASSES
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT