Question

I need help with a Python program that has error checking loops. Hour must be a...

I need help with a Python program that has error checking loops. Hour must be a number from 1 to 12. Minute and second must be numbers from 0 to 59. Also check whether “AM” or “PM” is entered. Every time an invalid value is entered, display an error message and ask the user to re-enter a valid value immediately. Functions are not covered yet and are not necessary. Thanks.

The following is an example:
Enter hour: 0
Hour must be from 1 to 12.
Enter hour: 13
Hour must be from 1 to 12.
Enter hour: 12
Enter minute: -1
Minute must be from 0 to 59.
Enter minute: 60
Minute must be from 0 to 59.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
hours = int(input("Enter hour: "))
while (hours<1) or (hours>12):
    print("Hour must be from 1 to 12.")
    hours = int(input("Enter hour: "))
minute = int(input("Enter minute: "))
while (minute<0) or (minute>59):
    print("Hour must be from 0 to 59.")
    minute = int(input("Enter minute: "))
n = input("Enter AM/PM: ")
while (n.lower()!="am") and (n.lower()!="pm"):
    print("It must be either AM or PM")
    n = input("Enter AM/PM: ")
print(hours,":",minute,"",n)

Output:

Enter hour: 0 Hour must be from 1 to 12. Enter hour: 13 Hour must be from 1 to 12. Enter hour: 12 Enter minute: -1 Hour must

Add a comment
Know the answer?
Add Answer to:
I need help with a Python program that has error checking loops. Hour must be a...
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