Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
534 views
in Technique[技术] by (71.8m points)

python - Issue writing a function which determines if a date is in the future

I'm trying to to define a function called inTheFuture() that accepts a given year number, a month number, and a day number as 3 separate arguments. The function should return a Boolean value (True or False) to indicate whether the date (year, month, and day) parameters are in the future or not.

This is the error message I keep getting:

TypeError: float() argument must be a string or a number, not 'tuple'

I have tried converting float, string, int, and I'm just at a loss.

This is my code:

import sys
import datetime

year= input ("Enter Year: ");
month= int(input ("Enter Month: "));
day= int(input ("Enter Day: "));

def getTodaysDate():
    return datetime.datetime.today();
today = getTodaysDate();

def inTheFuture():
    ymd=(year,month,day)
    if float(ymd)>today:
        return true
    if float(ymd)<today:
        return false

inf = inTheFuture();
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

To work with dates (without time), you could use datetime.date class:

#!/usr/bin/env python3
from datetime import date

def in_future(date_to_test):
    """Whether *date_to_test* is in the future."""
    return date_to_test > date.today()

input_date = date(*map(int, input("Enter Year-Month-Day: ").split('-')))
print("Got {}. Is it in the future?".format(input_date))
print("yup" if in_future(input_date) else "nope")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...