Multiple inputs on the same line (beginner) - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Multiple inputs on the same line (beginner) (/thread-34811.html) |
Multiple inputs on the same line (beginner) - dementshuk - Sep-02-2021 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? RE: Multiple inputs on the same line (beginner) - bowlofred - Sep-02-2021 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. RE: Multiple inputs on the same line (beginner) - menator01 - Sep-02-2021 Might could do something like this: #! /usr/bin/env python3 bday = input("Enter bday format 20/01/1991: ").split() print("/".join(bday))
RE: Multiple inputs on the same line (beginner) - deanhystad - Sep-02-2021 It sounds like the dateutil library could be helpefull here. https://pypi.org/project/python-dateutil/ Quote:parser Sound like you could parse the user input to get a datetime object and print the datetime object in whatever format you like. RE: Multiple inputs on the same line (beginner) - bowlofred - Sep-02-2021 (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(). RE: Multiple inputs on the same line (beginner) - deanhystad - Sep-02-2021 Got it.. automatically type / after the user types 20 and after 01. Easy to do in a gui. Not easy for a condole app RE: Multiple inputs on the same line (beginner) - Pedroski55 - Sep-03-2021 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 + '/' + yearCould 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() getDOBs()dob_dict looks like: Quote:>>> dob_dict RE: Multiple inputs on the same line (beginner) - naughtyCat - Sep-03-2021 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 RE: Multiple inputs on the same line (beginner) - DeaD_EyE - Sep-03-2021 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) RE: Multiple inputs on the same line (beginner) - dementshuk - Sep-03-2021 (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. 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 |