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)
How can I ask the user to insert a date in Python and automatically show the bar for him on the same line?

Something like:

> Insert your birthday: 20/01/1991 # input ('Insert your birthday:')

User writes only '20', '01' and '1991'.
As if you had 3 inputs appearing on the same line. Is it possible?
You can't do it with input(). You'd probably need to use something like curses, which will let you get single-key input and draw characters on the screen where you want. But I don't know of anything ready-made that is similar.
Might could do something like this:
#! /usr/bin/env python3

bday = input("Enter bday format 20/01/1991: ").split()
Enter bday format 20/01/1991: 20 01 1991 20/01/1991
I welcome all feedback.
The only dumb question, is one that doesn't get asked.

My Scripts
CookBook - Shmup - PyQt5 Music Player

It sounds like the dateutil library could be helpefull here.

This module offers a generic date/time string parser which is able to parse most known formats to represent a date and/or time.

Sound like you could parse the user input to get a datetime object and print the datetime object in whatever format you like.
(Sep-02-2021, 07:26 PM)deanhystad Wrote: It sounds like the dateutil library could be helpefull here.

I read the question not as parsing the input but as how to have the field separators (slashes) appear prior to the typing. Can't do that with input().
Got it.. automatically type / after the user types 20 and after 01. Easy to do in a gui. Not easy for a condole app
dementshuk likes this post
I'd go with what menator01 said.

Below is just a variation on his theme.

day, month, year = [x for x in input("enter day, month, year values, separated by a space : ") .split() ]
dob = day + '/' + month + '/' + year
Could add more, like first name and surname and make a dictionary:

dob_dict = {}

# a little function to save the name and dob
def getDOBs():
    fname, sname, day, month, year = [x for x in input("enter 1 firstname, surname, day, month, year values, separated by a space : ") .split() ]
    name  = fname +  ' ' + sname
    dob_dict[name] = day + '/' + month + '/' + year
    print('added', name + ' to the dob dictionary')

# just enter getDOBs()

dob_dict looks like:

Quote:>>> dob_dict
{'Peter Smith': '22/02/2020', 'John Jones': '23/03/1990'}
day, month, year = input().split() # input as string 
day, month, year = map(int, input().split()) # input as integer
birthday = list(map(int, input().split())) # input as a list
Pedroski55 likes this post
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():
    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): ")
            # 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)
            # 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.
dementshuk and Pedroski55 like this post
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die:
All humans together. We don't need politicians!
(Sep-02-2021, 07:44 PM)bowlofred Wrote:
(Sep-02-2021, 07:26 PM)deanhystad Wrote: It sounds like the dateutil library could be helpefull here.

I read the question not as parsing the input but as how to have the field separators (slashes) appear prior to the typing. Can't do that with input().

Yes, tha is what I want. As the user types the slashes appear... I even thought that input() would not do that. I'm going to look for the library. Thanks

Possibly Related Threads…
Thread Author Replies Views Last Post
  Generate Multiple sql Files With csv inputs vkomarag 13 785 Aug-20-2021, 07:03 PM
Last Post: vkomarag
  beginner text formatting single line to column jafrost 4 1,159 Apr-28-2021, 07:03 PM
Last Post: jafrost
  Multiple Line Chart Plotting moto17 1 1,021 Jan-20-2021, 01:38 PM
Last Post: wostan
  How to print string multiple times on new line ace19887 7 1,917 Sep-30-2020, 02:53 PM
Last Post: buran
  Taking Multiple Command Line Argument Input bwdu 6 1,691 Mar-29-2020, 05:52 PM
Last Post: buran
  Help with applying this instrument monitoring code to multiple inputs. Kid_Meier 1 774 Mar-04-2020, 12:01 PM
Last Post: Kid_Meier
  Problem with accepting multiple string inputs Ryan_Todd 5 1,214 Jan-22-2020, 06:12 PM
Last Post: buran
  Multiple Line Input helenaxoxo 4 1,267 Dec-02-2019, 11:06 PM
Last Post: helenaxoxo
  Trying to prompt user for 2 inputs on one line pythonprogrammer 2 1,184 Sep-15-2019, 04:41 PM
Last Post: snippsat
  Watching for inputs from multiple sources concurrently anakaine 4 1,503 Mar-03-2019, 07:38 PM
Last Post: woooee

Forum Jump:

User Panel Messages

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