Question

Python Program

5. Write a Python program in a file named validTime.py. Include a function named string parameter of the form hh:mm: ss in wh

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

def validTime(time):
    """
    function to validate time
    :param time:
    :return:
    """
    # valid time must have length of 8 , 6 digit and 2 :
    if len(time) != 8:
        return False
    # valid time must have the two :
    data = time.strip().split(':')
    if len(data) < 3:
        return False
    try:
        # valid time must be integer
        hr = int(data[0])
        mnt = int(data[1])
        sec = int(data[2])
        # checking the limit
        if hr < 0 or hr > 23:
            raise
        elif mnt < 0 or mnt > 59:
            raise
        elif sec < 0 or sec > 59:
            raise
    except:
        return False

    return True


def main():
    t1 = "22:59:59"
    t2 = "21:0:56"
    t3 = "24:02:34"
    print("Is %s valid time  %s " % (t1, validTime(t1)))
    print("Is %s valid time  %s " % (t2, validTime(t2)))
    print("Is %s valid time  %s " % (t3, validTime(t3)))


if __name__ == '__main__':
    main()

# OUT

Is 22:59:59 valid time True
Is 21:0:56 valid time False
Is 24:02:34 valid time False

Add a comment
Know the answer?
Add Answer to:
Python Program 5. Write a Python program in a file named validTime.py. Include a function named...
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
  • Write a Python program which defines a function named "divide_two_numbers" that accepts two numeric parameters and...

    Write a Python program which defines a function named "divide_two_numbers" that accepts two numeric parameters and divides the first parameter by the second. Your divide_two_numbers function must check the second parameter before dividing; if it is zero, raise an ZeroDivisionError exception which includes the string "second parameter was zero!" as a parameter instead of processing the division. Your main function must call your divide_two_numbers function at least twice; one call must use two normal parameters to verify normal operation, the...

  • Write a python function named displayFizzBuzz that takes a number as a parameter and DISPLAYS the...

    Write a python function named displayFizzBuzz that takes a number as a parameter and DISPLAYS the appropriate FizzBuzz value for that number. Test this function on all of the numbers between 1 and 100000. Submit the code and a screenshot of the program running in Linux

  • PYTHON 3!!!!! Write a function: class Solution { public String solution(String T); } that, given a...

    PYTHON 3!!!!! Write a function: class Solution { public String solution(String T); } that, given a string T, returns the latest valid time that can be obtained from T, as a string in the format "HH:MM", where HH denotes a two-digit value for hours and MM denotes a two-digit value for minutes. Examples: 1. Given T = "2?:?8", the function should return "23:58". 2. Given T = "?8:4?", the function should return "18:49". 3. Given T = "??:??", the function...

  • PYTHON Write a program that converts a total number of seconds to hours, minutes, and seconds....

    PYTHON Write a program that converts a total number of seconds to hours, minutes, and seconds. It should (1) prompt the user for input, (2) read an integer from the keyboard, (3) and calculate the result. For example, "5000 seconds = 1 hours, 23 minutes, and 20 seconds". Hint: Use the modulus operator.

  • (I would like this to be in five parts.) Write a program (Python module) that includes...

    (I would like this to be in five parts.) Write a program (Python module) that includes a function named studentrecords. This function must support execution of two loops. The first loop is to query the user whether they wish to enter additional records or not. It must accept responses of either yes or no as input, regardless of the letter-cases used for individual letters in the response. If the response is not recognized ( it isn’t yes or no) it...

  • INSTRUCTION AND PROBLEMS Write a Python program for each of the problems in this lab. Please...

    INSTRUCTION AND PROBLEMS Write a Python program for each of the problems in this lab. Please use PyCharm to type and test your programs. Submit the Python files to Blackboard for credit. In this lab, you should submit 4 Python files, one for each problem PROBLEM I Energy consumption is measured in units of kilowatt hours (kWh). The more kWh a household use in a month, the higher the energy bll. A power company charges customers $0.12 per kWh for...

  • Can someone solve it with C plz Write a program that inputs a time from the...

    Can someone solve it with C plz Write a program that inputs a time from the console. The time should be in the format “HH:MM AM” or “HH:MM PM”. Hours may be one or two digits, for example, “1:10 AM” or “11:30 PM”. Your program should include a function that takes a string parameter containing the time. This function should convert the time into a four digit military time based on a 24 hour clock. For example, “1:10 AM” would...

  • Using Python Write a program that reads an int N >= 0, then prints out each...

    Using Python Write a program that reads an int N >= 0, then prints out each of the following patterns of *. Here, you may use the * repetition operator, if you wish. Also, each pattern must be output via a single function call to the given named function with single parameter N. Examples for N==4 follow, with explanations of how the displayed diagram reflects this value of N: ) Square with number diagonals, with outer side length of N,...

  • 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...

  • PROJECT 6    Functions You are to write a PYTHON program which: *Defines the following 4  functions: whoamI()...

    PROJECT 6    Functions You are to write a PYTHON program which: *Defines the following 4  functions: whoamI() This function prints out your name and lists any programming course you have taken prior to this course. isEven(number) This function accepts one parameter, a number, and prints out a message indicating if the number is even or odd. Keep in mind that a number is even if there is no remainder when the number is divided by two. printEven(number) This function accepts one...

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