如何设置功能

时间:2019-04-03 23:06:43

标签: python python-3.x

我是Stack Overflow的新手,如果格式不好,抱歉。我不确定如何启动该程序并使用多种功能运行。我不是要为我做功课。我只是想知道如何调用函数,或者是否可以为我指出正确的方向来运行此代码,谢谢!

from datetime import datetime

mph = 0

def main():
    print("Arrival Time Estimator\n")


def get_departure_date(arrival_time):
    while True:
        date_str = input("Enter departure date (YYYY-MM-DD): ")
        try:
            departure_date = datetime.strptime(date_str, "%Y-%m-%d")
        except ValueError:
            print("Invalid date format. Try again. ")
            continue

        if departure_date <= get_departure_time():
            print("Departure date must be after arrival date. " +
                  "Try again.")
            continue
        else:
            return departure_date

def get_departure_time():
    while True:
        time_str = input("Enter departure time (HH:MM AM/PM): ")
        try:
            depart_time = datetime.strptime(time_str, "%H-%M %p")
        except ValueError: 
            print("Invalid date format. Try again.")
            continue

        now = datetime.now
        arrivaldate = get_departure_time() + (miles / mph)
        today = datetime(now.year, now.month, now.day)
        if depart_time < today:
            print("Arrival date must be today or later. Try again.")
            continue
        else:
            return arrival()

def arrival():
    miles = input("Enter miles:  ")
    mph = input("Enter miles per hour  ")
    arrivaldate = get_departure_time() + (miles / mph)

1 个答案:

答案 0 :(得分:3)

要调用python函数,请使用函数名称。例如,如果您的函数是:

def arrival():
    miles = input("Enter miles:  ")
    mph = input("Enter miles per hour  ")
    arrivaldate = get_departure_time() + (miles / mph)

您可以通过致电

使用它
arrival()