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
  • Python 3.7 to be used. Just a simple design a program that depends on its own. You should also not need to import anythi...

    Python 3.7 to be used. Just a simple design a program that depends on its own. You should also not need to import anything. No code outside of a function! Median List Traversal and Exception Handling Create a menu-driven program that will accept a collection of non-negative integers from the keyboard, calculate the mean and median values and display those values on the screen. Your menu should have 6 options 50% below 50% above Add a number to the list/array...

  • Case Problem 11 - 1: Modify the GreenvilleRevenue program created in the previous chapter so that...

    Case Problem 11 - 1: Modify the GreenvilleRevenue program created in the previous chapter so that it performs the following tasks: The program prompts the user for the number of contestants in this year’s competition; the number must be between 0 and 30. Use exception-handling techniques to ensure a valid value and display the error message: Number must be between 0 and 30 The program prompts the user for talent codes. Use exception-handling techniques to ensure a valid code and...

  • Write a program named CheckMonth2 that prompts a user to enter a birth month and day....

    Write a program named CheckMonth2 that prompts a user to enter a birth month and day. Display an error message that says Invalid date if the month is invalid (not 1 through 12) or the day is invalid for the month (for example, not between 1 and 31 for January or between 1 and 29 for February). If the month and day are valid, display them with a message. For example, if the month entered is 2, and the day...

  • This lab will create a digital clock based on nested FOR loops for hours, minutes, and...

    This lab will create a digital clock based on nested FOR loops for hours, minutes, and seconds, and a WHILE loop for doing over and over so that your clock works for all year long without interruption. Note: If a group of students is interested, we could create a real digital wall-mounted clock to be placed in our classroom, so I can keep track of the time during my lectures. The actual clock would be based on the LED Strip...

  • Write a program in visual c# program named CheckMonth that prompts a user to enter a...

    Write a program in visual c# program named CheckMonth that prompts a user to enter a birth month. If the value entered is greater than 12 or less than 1, display an error message; otherwise, display the valid month with a message such as 3 is a valid month.

  • Write a program that reads a string from the keyboard and tests whether it contains a...

    Write a program that reads a string from the keyboard and tests whether it contains a valid time. Display the time as described below if it is valid, otherwise display a message as described below. The input date should have the format hh:mm:ss (where hh = hour, mm = minutes and ss = seconds in a 24 hour clock, for example 23:47:55). Here are the input errors (exceptions) that your program should detect and deal with: Receive the input from...

  • Lesson is about Input validation, throwing exceptions, overriding toString Here is what I am supposed to...

    Lesson is about Input validation, throwing exceptions, overriding toString Here is what I am supposed to do in the JAVA Language: Model your code from the Time Class Case Study in Chapter 8 of Java How to Program, Late Objects (11th Edition) by Dietel (except the displayTime method and the toUniversalString method). Use a default constructor. The set method will validate that the hourly employee’s rate of pay is not less than $15.00/hour and not greater than $30.00 and validate...

  • OBJECTIVES: To understand repetition structures To use while loops in a program To understand break and/or...

    OBJECTIVES: To understand repetition structures To use while loops in a program To understand break and/or continue instructions Write a Python program (with comments) to do the following: Create a variable count and initialize it to 0 Create a variable colors and initialize it to [] (i.e. an empty list) Ask the user if they would like to enter up to 3 favorite colors. They should enter ‘y’ or ‘n’. Assume they will enter a valid input. Save the response...

  • USING PYTHON - Write a program that calls a function that prompts the user to enter...

    USING PYTHON - Write a program that calls a function that prompts the user to enter a real number. The function should verify that the user entered a valid real number, and should prompt the user to re-enter any invalid input. After a valid real number has been entered, the function should return the number as a number. To test your function, from a main function, call the function two separate times and print the sum of the two numbers...

  • This is a matlab HW that I need the code for, if someone could help me figure this out it would b...

    This is a matlab HW that I need the code for, if someone could help me figure this out it would be appreciated. The value of t can be estimated from the following equation: in your script file, estimate the value of π for any number of terms. You must ask the user for the desired number of terms and calculate the absolute error/difference between your calculation and the built-in MATLAB value ofpi. Display your results with the following message...

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