Question

In Python 3, write a function that will take an integer which represents a time given...

In Python 3, write a function that will take an integer which represents a time given in military time (0 – 2359) and return a string that represents the time in a 12-hour format. If an invalid time is given, the function must return the string “Invalid time”.

Examples

1200 will return the string “12:00 PM”

2240 will return the string “10:40 PM”

0600 will return the string “06:00 AM”

2400 will return the string “Invalid time”

1160 will return the string “Invalid time”

Write automated test cases to sufficiently test your function. Consider all possible execution paths throughout your function. The automated tests must determine if the tests have successfully passed or not.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

def convertTime(time):

hour=int(time[:2])

mint=int(time[2:])

h=""

m=""

if(hour<10):

h="0"+str(hour)

else:

h=str(hour)

if(mint<10):

m="0"+str(mint)

else:

m=str(mint)

if(hour<0 or hour>23 or mint<0 or mint>59 ):

return "Invalid Time"

if(hour<13):

return str(h)+" : "+str(m)+" AM"

return str(m)+" : "+str(m)+" PM"



print(convertTime("1200"))

print(convertTime("2240"))

print(convertTime("0600"))

Add a comment
Know the answer?
Add Answer to:
In Python 3, write a function that will take an integer which represents a time given...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT