This function works like a checklist. It takes a task name and date in the format yyyy/mm/dd HH:MM:SS
, and appends them to a list as a tuple. However, when I print them out there is a problem.
When printing the tuple it look like ("A", 2022/12/08 22:15:56)
. What I am actually seeing is something like ('A', datetime.datetime(2022, 12, 8, 22, 23, 56)
.
The second problem I have is how to retrieve the values from inside the tuple for use in the function. I don't want them to be printed like ("A",2022/12/08 22:15:56)
. I want them to be outside of the tuple parenthesis.
from datetime import datetime
ToDoList = []
Format_Time = "%Y/%m/%d %H:%M:%S" # formatting type for strptime function
def addNewItems():
global ToDoList
while True:
name = input("Pleas enter your task's name: ")
if checkItemExists(name) == True:
print("Task already exists! Select the options again.")
continue
else:
Task_Name = name # storing in another variable
break
while True:
date = input(
"Please enter your task's completion date as yyyy/mm/dd HH:MM:SS : ")
date = datetime.strptime(date, Format_Time)
if date < datetime.now(): # determines if the date is in the past or not
print("Time entered is in the past! Select the options again")
continue
else:
Complete_Time = date # Storing in another variable
print(Complete_Time)
break
task = (Task_Name, Complete_Time)
ToDoList.append(task)
ToDoList.sort(key=takeSecond)
def checkItemExists(taskName):
global ToDoList
for i in ToDoList:
if taskName == i[0]:
return True
def takeSecond(element):
global ToDoList
for i in ToDoList:
if element == i[0]:
return i[1]
def printListItems():
global ToDoList
for count, task in enumerate(ToDoList):
print(count, end=".")
print(task)
CodePudding user response:
Looks like you're just missing some fundamental ideas. Running datetime.now()
creates a datetime
object, as you're seeing when you look at ('A', datetime.datetime(2022, 12, 8, 22, 23, 56)
Python 3.10.6 (tags/v3.10.6:9c7b4bd, Aug 1 2022, 21:53:49) [MSC v.1932 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from datetime import datetime
>>> dt = datetime.now()
>>> type(dt)
<class 'datetime.datetime'>
>>> dt
datetime.datetime(2022, 11, 9, 15, 45, 2, 88861)
The datetime object allows you to do all kinds of cool operations and comparisons, and you just need to convert it to str
when you're ready to show it to a human. This is handled automatically by print
or you can cast it directly as str
if you want to get fancier.
>>> dt.year # Cool thing
2022
>>>> print(dt)
2022-11-09 15:45:02.088861
>>> str(dt)
'2022-11-09 15:45:02.088861'
>>> print("The time was " dt) # This will fail
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "datetime.datetime") to str
>>> print("The time was " str(dt)) # This will work
The time was 2022-11-09 15:45:02.088861
As for the tuple
thing, you can access the items just like you can with lists:
>>> mytup
('Foo', 'Bar')
>>> mytup[0]
'Foo'
>>> mytup[1]
'Bar'
CodePudding user response:
Your first misunderstanding is what happens when you do this (in the REPL):
>>> format_time = "%Y/%m/%d %H:%M:%S"
>>> my_tuple = (1, datetime.strptime("1919/04/13 12:00:00", format_time))
>>> print(my_tuple)
(1, datetime.datetime(1919, 4, 13, 12, 0))
This prints these values as they think of themselves. That is why you see datetime.datetime(..)
part of the printout. When you call strptime()
you are converting a string "1919/04/13 12:00:00"
type object into a datetime type object.
Notably, the way that happens is via format_time
, which is the template that strptime()
uses to interpret the string. When print()
is called, it asks the datetime object for a string that represents itself. That string includes the object type (datetime.datetime
), which is what print()
is then showing on your screen.
The datetime object contains some functions to help you out:
print(my_tuple[1].("The time is %d, %b, %Y)")
For your second question:
>>> my_tuple = ("Hello", "World")
>>> print(my_tuple)
('Hello', 'World')
>>> h, w = my_tuple
>>> print(f"{w}, {h}")
World, Hello
CodePudding user response:
Both problems are solved by changing your very last line to
print (task[0],task[1])
which makes the elements available for formatting. With no additions it would look like
Fix next bug 2022-11-10 10:20:22