Question
Use Python and please comments every step. [25] 6. Write a program that reads from standard...
![[25] 6. Write a program that reads from standard input, two times in military format and prints the number of hours and minut](https://i.imgur.com/IFa4muY.png)
Answers
CodeToCopy:
timediff.py
#python 2.7
# taking time1 from user
time1 = raw_input("Please enter the first time: ")
# taking time2 from user
time2 = raw_input("Please enter the second time: ")
# convertin them as integers
time1 = int(time1)
time2 = int(time2)
# fetching minutes from the time1
mins1 = time1 % 100
# fetching minutes from the time2
mins2 = time2 % 100
# fetching hours from the time1
hours1 = time1/100
# fetching hours from the time2
hours2 = time2/100
# calculating hour difference between time2 and time1
diff_hours = (hours2 - hours1) % 24
# minute difference between time2 and time1
diff_mins = (mins2 - mins1);
# if minute difference is less than 0, hours need to be reduced by 1 to
# get actual hour difference
diff_hours = (diff_hours, diff_hours-1) [ diff_mins < 0]
# finalizing the range of minute difference time2 and time1
diff_mins = diff_mins % 60
# printing difference between two times
print("Difference between the two times: " + str(diff_hours) + " hours and " + str(diff_mins) + " minutes.")
timediff.py
#python 3.x
# taking time1 from user
time1 = input("Please enter the first time: ")
# taking time2 from user
time2 = input("Please enter the second time: ")
# convertin them as integers
time1 = int(time1)
time2 = int(time2)
# fetching minutes from the time1
mins1 = time1 % 100
# fetching minutes from the time2
mins2 = time2 % 100
# fetching hours from the time1
hours1 = time1/100
# fetching hours from the time2
hours2 = time2/100
# calculating hour difference between time2 and time1
diff_hours = (hours2 - hours1) % 24
# minute difference between time2 and time1
diff_mins = (mins2 - mins1);
# get actual hour difference
diff_hours = (diff_hours, diff_hours-1) [ diff_mins < 0]
# finalizing the range of minute difference time2 and time1
diff_mins = diff_mins % 60
# printing difference between two times
print("Difference between the two times: " + str(diff_hours) + " hours and " + str(diff_mins) + " minutes.")
OutputScreenshot:
File Edit View Search Terminal Help [email protected]:~/Desktop/personal/python$ python timediff.py Please enter the first time: 0900 Please enter the second time: 1730 Difference between the two times: 8 hours and 30 minutes. [email protected]:~/Desktop/personal/python$ python timediff.py Please enter the first time: 1730 Please enter the second time: 0900 Difference between the two times: 15 hours and 30 minutes. [email protected]:~/Desktop/personal/python$
File Edit View Search Terminal Help [email protected]:~/Desktop/personal/python$ python timediff.py Please enter the first time: 0900 Please enter the second time: 1730 Difference between the two times: 8 hours and 30 minutes. [email protected]:~/Desktop/personal/python$ python timediff.py Please enter the first time: 1730 Please enter the second time: 0900 Difference between the two times: 15 hours and 30 minutes. [email protected]:~/Desktop/personal/python$