Python Forum
Multiple inputs on the same line (beginner)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multiple inputs on the same line (beginner)
#9
Information 
You can use datetime.datetime.strptime.

If you want that the user must enter Month Day Year, use as format_str "%m %d %y"
The format in my example: "%m/%d/%y" -> 12/31/21

%m = Month as a zero-padded decimal number.
%y = Year without century as a zero-padded decimal number.
%d = Day of the month as a zero-padded decimal number.

import datetime


def ask_date():
    # https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes
    dt_format = "%m/%d/%y"

    # loop until the user inputs a valid date specified by
    # dt_format

    while True:
        user_input = input("Please enter the date (MM/DD/YY): ")
        try:
            # if strptime could not convert the str, then
            # a ValueError is raised as Exception
            date = datetime.datetime.strptime(user_input, dt_format).date()
            # datetime.datetime.strptime returns a datetime object and the method
            # date on the datetime object returns a date object derrived from datetime object
            # why? the date class has no Method for strptime because a date object could not store
            # a time, only dates.
        except ValueError:
            # input was invalid format
            # catching this Exception and printing an error
            print("Invalid date:", user_input)
        else:
            # the else block is only executed, if inside the
            # try-block no exception was thrown
            # return this new date object from this function
            # which leaves the while loop and leaves the function
            return date


# calling the function and assign the returned object to a name
user_date = ask_date() # this function repeats asking this question until the user has entered a valid date

# here you can work with user_date, which is a date object.
print(user_date)
dementshuk and Pedroski55 like this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
RE: Multiple inputs on the same line (beginner) - by DeaD_EyE - Sep-03-2021, 11:11 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Multiple variable inputs when only one is called for ChrisDall 2 508 Oct-20-2023, 07:43 PM
Last Post: deanhystad
  Trying to loop through code to plot seaborn line plots across multiple subplots eyavuz21 0 1,714 Dec-05-2022, 10:46 AM
Last Post: eyavuz21
  Generate Multiple sql Files With csv inputs vkomarag 13 4,274 Aug-20-2021, 07:03 PM
Last Post: vkomarag
  beginner text formatting single line to column jafrost 4 3,249 Apr-28-2021, 07:03 PM
Last Post: jafrost
  Multiple Line Chart Plotting moto17 1 2,521 Jan-20-2021, 01:38 PM
Last Post: wostan
  How to print string multiple times on new line ace19887 7 5,817 Sep-30-2020, 02:53 PM
Last Post: buran
  Taking Multiple Command Line Argument Input bwdu 6 4,087 Mar-29-2020, 05:52 PM
Last Post: buran
  Help with applying this instrument monitoring code to multiple inputs. Kid_Meier 1 2,116 Mar-04-2020, 12:01 PM
Last Post: Kid_Meier
  Problem with accepting multiple string inputs Ryan_Todd 5 2,985 Jan-22-2020, 06:12 PM
Last Post: buran
  Multiple Line Input helenaxoxo 4 2,964 Dec-02-2019, 11:06 PM
Last Post: helenaxoxo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020