mardi 2 novembre 2021

Python program not printing number of days - datetime module

I am working on an assignment for my Python class that returns the number of days that have passed since Pearl Harbor going off of what date the user inputs. If the user inputs a date that is before 12/07/1941 then they get an invalid entry message and have to try until the correct date is put in.

I have most of this complete but for some reason when the user enters a valid date that is after 12/07/1941 it doesn't print anything. Not sure what to do to correct it. Code is below

import datetime

pearlHarborDate = datetime.date(1941, 12, 7)

print("This program calculates the days between the date you input and the date of Pearl 
Harbor. Please only enter a date that is after 12/07/1941")
print()
userInput = input('Please enter a date (mm/dd/yyyy): ')
userDate = datetime.datetime.strptime(userInput,'%m/%d/%Y').date()
while userDate < pearlHarborDate:
    print("Invalid Entry! Please try again")
    if userDate >= pearlHarborDate:
        days = userDate - pearlHarborDate
        print(days)
    userInput = input('Please enter a date (mm/dd/yyyy): ')
    userDate = datetime.datetime.strptime(userInput,'%m/%d/%Y').date()

Aucun commentaire:

Enregistrer un commentaire