1

I have an issue with my Python code which works on Windows XP (Python 2.7):

from datetime import datetime
from datetime import date

def temps_valide(nbrjmax):
    try:
        datetime.strptime(nbrjmax, '%d/%m/%Y')
        return True
    except ValueError:
        return False
        
def nbrjour(nbrjmax):
    date_aujoudhui = datetime.now().date()
    delta = date_aujoudhui - format_date_ent
    return delta.days

boucle = "2"
while boucle == "2":
    test = "test"
    if test == "test":
        nbrjmax = input("Date (JJ/MM/AAAA) ? : ")
        while temps_valide(nbrjmax) == False:
            print("Entrez un format correct (JJ/MM/AAAA)")
            nbrjmax = input("Date (JJ/MM/AAAA)? : ")
        else:
            format_date_ent = datetime.strptime(nbrjmax, '%d/%m/%Y').date()
            nbrjmax = nbrjour(nbrjmax)
    else:
        nbrjmax = 0
    print(nbrjmax)

With Python 3.9, this code works perfectly but not on 2.7 version.

When I add str(nbrjmax) on the function temps_valide, I'm stuck in the while loop, like the verify don't work.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Marks
  • 19
  • 2
  • You want `raw_input`, not `input` in Python 2. You should learn about the differences; these are fundamentally two different languages, though obviously in many ways quite similar. – tripleee Jan 31 '23 at 12:51
  • Maybe also look into a compatibility library like `six` if you want to write code which works with both Python 2 and Python 3. – tripleee Jan 31 '23 at 12:52
  • 1
    Thank you, I was stuck on it. Yeah I know that's two different languages but I used to work on 3.9 Version and sometimes I need to work on older version. – Marks Jan 31 '23 at 12:54

0 Answers0