Exercise: Greeting, Depending on Time of Day

A greeting has to be composed automatically, based upon time of day and sex.

  • Use the input() function to prompt the user on stdin for three variables,

    • Name

    • Sex. Valid inputs are m (male) and f (female); both uppercase and lowercase characters are allowed. All other input leads to program termination (use sys.exit(1) for erroneous termination).

    • Hour of day. Valid inputs are number 0 through 23; all other input leads to program termination.

  • Depending on the hour of day, the greeting starts with

    • 0 - 9: “Good morning, “

    • 10 - 17: “Good day, “

    • 18 - 23: “Good evening, “

  • Depending on the sex, the greeting continues with “Mrs.” or “Mr.”.

  • Last comes the name.

  • Finally, the greeting is output on stdout

$ python greet.py
Name: Jörg
Sex: f
Hour of day: 22

Good evening, Mrs. Jörg

Dependencies

cluster_python Python Programming cluster_python_exercises Exercises cluster_python_exercises_herdt Exercises (External) cluster_python_basics Python: The Language Fundamentals python_exercises_herdt_boolean Exercise: Boolean Expressions python_basics_python_0160_boolean Boolean python_exercises_herdt_boolean->python_basics_python_0160_boolean python_exercises_herdt_swap Exercise: Swap Two Variables python_exercises_herdt_swap->python_exercises_herdt_boolean python_basics_python_0120_helloworld Hello World python_exercises_herdt_swap->python_basics_python_0120_helloworld python_basics_python_0150_datatypes_overview Datatypes python_exercises_herdt_swap->python_basics_python_0150_datatypes_overview python_basics_python_0140_variables Variables python_exercises_herdt_swap->python_basics_python_0140_variables python_exercises_herdt_greeting Exercise: Greeting, Depending on Time of Day python_exercises_herdt_max_numbers Exercise: Determine Maximum of Two Numbers python_exercises_herdt_greeting->python_exercises_herdt_max_numbers python_basics_python_0170_if The if Statement python_exercises_herdt_greeting->python_basics_python_0170_if python_exercises_herdt_max_numbers->python_exercises_herdt_swap python_exercises_herdt_max_numbers->python_basics_python_0170_if python_basics_python_0139_commandline_argv Commandline Arguments (sys.argv) python_exercises_herdt_max_numbers->python_basics_python_0139_commandline_argv python_exercises_herdt_max_numbers->python_basics_python_0140_variables python_basics_python_0110_blahblah Blahblah python_basics_python_0120_helloworld->python_basics_python_0110_blahblah python_basics_python_0160_boolean->python_basics_python_0150_datatypes_overview python_basics_python_0170_if->python_basics_python_0160_boolean python_basics_python_0150_datatypes_overview->python_basics_python_0140_variables python_basics_python_0130_syntax_etc Syntax etc. python_basics_python_0130_syntax_etc->python_basics_python_0120_helloworld python_basics_python_0139_commandline_argv->python_basics_python_0130_syntax_etc python_basics_python_0125_running Running Python Programs python_basics_python_0139_commandline_argv->python_basics_python_0125_running python_basics_python_0140_variables->python_basics_python_0130_syntax_etc python_basics_python_0125_running->python_basics_python_0120_helloworld