Question

In: Computer Science

Complete the Python function called 'greetings(time)' that accepts a time in "HH:MM" format as a string....

Complete the Python function called 'greetings(time)' that accepts a time in "HH:MM" format as a string. The function should return a greeting message based on the hour given in the time object as follow:

If the time is before noon: 'Good Morning.', if it is in the afternoon: 'Good Day.'

Please use the following template for your python script to define the greetings() function and name it as mt_q3.py. Replace the place holder [seneca_id] with your Seneca email user name. You are allowed to use any built-in functions in the greetings() function.

#!/usr/bin/env python3

# program: mt_q3.py

# author_id: [seneca_id]

def greetings(time):
    greeting_message = ''
    # put code below to set the appropriate greeting message based on the time of the day
    # before noon: Good Morning.
    # after noon: Good Day.

    return greeting_message

if __name__ == '__main__':
    time = input('What is the time in HH:MM? ')
    print('Hello,',greetings(time))

Note: In the output of the following sample run, bold face characters in red color are typed in by user on the keyboard.
Sample run of the mt_q3.py script:


[rchan@centos7 mt_test]$ python3 mt_q3.py
What is the time in HH:MM? 00:01
Hello, Good Morning.
[rchan@centos7 mt_test]$ python3 mt_q3.py
What is the time in HH:MM? 11:59
Hello, Good Morning.
[rchan@centos7 mt_test]$ python3 mt_q3.py
What is the time in HH:MM? 12:00
Hello, Good Day.
[rchan@centos7 mt_test]$ python3 mt_q3.py
What is the time in HH:MM? 23:59
Hello, Good Day.

Solutions

Expert Solution

Please find below code and don't forget to give a Like.

Please refer below screenshot for indentation and output:

code:

def greetings(time):
    if(len(time)==1 or len(time)==2):
        return "Not valid timings"
    greeting_message = ''
    index1=time.index(":") #getting the index value for getting hours
    last=len(time)
    mins=int(time[index1+1:last]) #for mins validation 0 to 59 only
    hours=int(time[0:index1])# getting hours from 1st letter to till :
    #condition for good morning
    if((hours>=00 and hours<=11) and(mins>=0 and mins<=59)):
        greeting_message+="Good Morning."
    #condition for good day
    elif((hours>11 and hours<24)and(mins>=0 and mins<=59)):
        greeting_message+="Good Day."
    #if time is not valid
    else:
        greeting_message+="Not valid timings"
    return greeting_message

if __name__ == '__main__':
    time = input('What is the time in HH:MM? ')
    print('Hello,',greetings(time))

Output:


Related Solutions

Write a function called format_name that accepts a string in the format of first name followed...
Write a function called format_name that accepts a string in the format of first name followed by last name as a parameter, and then returns a string in reverse order (i.e., last name, first name). You may assume that only a first and last name will be given. For example, format_name("Jared Smith") should return "Smith, Jared" Hint: The following String Methods will be useful: # Returns the lowest index in the string where substring is # found. Returns -1 if...
Using Python #Write a function called after_second that accepts two #arguments: a target string to search,...
Using Python #Write a function called after_second that accepts two #arguments: a target string to search, and string to search #for. The function should return everything in the first #string *after* the *second* occurrence of the search term. #You can assume there will always be at least two #occurrences of the search term in the first string. # #For example: # after_second("11223344554321", "3") -> 44554321 # #The search term "3" appears at indices 4 and 5. So, this #returns everything...
Write a python function to fulfill the requirements. The function accepts a string, a current state,...
Write a python function to fulfill the requirements. The function accepts a string, a current state, edges’ information, and an accepting state. The output of the function is a boolean value verifying if the string can pass the finite state machine or not.             ### Finite State Machine Simulator in Python ### Provide s1 and s2 that are both accepted, but s1 != s2. s1 = "bdf" s2 = "bdgbdf" edges = {(1,'a') : 2,                (1,'b') : 3,       ...
In Python Create a function called ????. The function receives a "string" that represents a year...
In Python Create a function called ????. The function receives a "string" that represents a year (the variable with this "String" will be called uve) and a list containing "strings" representing bank accounts (call this list ????). • Each account is represented by 8 characters. The format of each account number is "** - ** - **", where the asterisks are replaced by numeric characters. o For example, “59-04-23”. • The two central characters of the "string" of each account...
Your code needs to do the following: Create a function called pigLatin that accepts a string...
Your code needs to do the following: Create a function called pigLatin that accepts a string of English words in the parameter sentence and returns a string of those words translated into Pig Latin. English is translated to Pig Latin by taking the first letter of every word, moving it to the end of the word and adding ‘ay’. For example the sentence “The quick brown fox” becomes “hetay uickqay rownbay oxfay”. You may assume the words in the parameter...
In python, write a function, called ThreeSum, that accepts a list of non-negative numbers as input,...
In python, write a function, called ThreeSum, that accepts a list of non-negative numbers as input, and returns the highest sum of three neighboring elements in it. Write a main method that initializes the following five lists, gets the ThreeSum result for all of them using the above function, and prints the result to the screen. Example of the output: List 1: [4,5,4,5] , Three sum = 14 List 2: [7] , Three sum = 7 List 3: [ ]...
Language: Python 3 Compose a function process which accepts a string filename. process should return a...
Language: Python 3 Compose a function process which accepts a string filename. process should return a list of records contained in the file. #define your function here # Create a blank list called `entries` and an empty string called `current_style`. # Open the file `filename`, read the data using readlines(), and close it. # Loop through each line of the file and do the following: # Strip the whitespace off of the ends of the line using the `strip` method....
Create a function called time2Greeting. It takes a time (in military time) and returns a string...
Create a function called time2Greeting. It takes a time (in military time) and returns a string with the right greeting. Good Morning 4AM to before noon. Good Afternoon Noon to before 5PM Good Evening from 5PM to 11PM What are you doing up at this hour? between 11 and 4AM For illegal values, say: That is not a valid time. Example: What is your name?   John What time is it? 1315 Good afternoon, John. C++ programming
Python program. Write a function called cleanLowerWord that receives a string as a parameter and returns...
Python program. Write a function called cleanLowerWord that receives a string as a parameter and returns a new string that is a copy of the parameter where all the lowercase letters are kept as such, uppercase letters are converted to lowercase, and everything else is deleted. For example, the function call cleanLowerWord("Hello, User 15!") should return the string "hellouser". For this, you can start by copying the following functions discussed in class into your file: # Checks if ch is...
Python program.  from random import . Write a function called cleanLowerWord that receives a string as a...
Python program.  from random import . Write a function called cleanLowerWord that receives a string as a parameter and returns a new string that is a copy of the parameter where all the lowercase letters are kept as such, uppercase letters are converted to lowercase, and everything else is deleted. For example, the function call cleanLowerWord("Hello, User 15!") should return the string "hellouser". For this, you can start by copying the following functions discussed in class into your file: # Checks...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT