Skip to main content
edited title
Link
UpAndAdam
  • 1.7k
  • 1
  • 3
  • 20

CS50P outdated Code: code not rejectecing badrejecting incorrectly formatted inputs

title fix and tags and ordering
Source Link
UpAndAdam
  • 1.7k
  • 1
  • 3
  • 20

CS50P outdated Code not resulting in mixed up date formatsrejectecing bad formatted inputs

import re

months = [
    "January",
    "February",
    "March",
    "April",
    "May",
    "June",
    "July",
    "August",
    "September",
    "October",
    "November",
    "December"
]

def main():
    conversion()


def conversion():
    pattern = r'[ /, ]+'
    while True:
        try:
            date = input("Date: ").strip()
            month, day, year = re.split(pattern, date)
            if month in months:
                month = months.index(month) + 1
            month = int(month)
            day = int(day)
            year = int(year)
            #print(day)
            if day > 31 or month > 12:
                pass
            else:
                print(f"{year}-{month:02}-{day:02}")
                break
        except ValueError:
            pass

main()
import re

months = [
    "January",
    "February",
    "March",
    "April",
    "May",
    "June",
    "July",
    "August",
    "September",
    "October",
    "November",
    "December"
]

def main():
    conversion()


def conversion():
    pattern = r'[ /, ]+'
    while True:
        try:
            date = input("Date: ").strip()
            month, day, year = re.split(pattern, date)
            if month in months:
                month = months.index(month) + 1
            month = int(month)
            day = int(day)
            year = int(year)
            #print(day)
            if day > 31 or month > 12:
                pass
            else:
                print(f"{year}-{month:02}-{day:02}")
                break
        except ValueError:
            pass

main()

CS50P outdated Code not resulting in mixed up date formats

import re

months = [
    "January",
    "February",
    "March",
    "April",
    "May",
    "June",
    "July",
    "August",
    "September",
    "October",
    "November",
    "December"
]

def main():
    conversion()


def conversion():
    pattern = r'[ /, ]+'
    while True:
        try:
            date = input("Date: ").strip()
            month, day, year = re.split(pattern, date)
            if month in months:
                month = months.index(month) + 1
            month = int(month)
            day = int(day)
            year = int(year)
            #print(day)
            if day > 31 or month > 12:
                pass
            else:
                print(f"{year}-{month:02}-{day:02}")
                break
        except ValueError:
            pass

main()

CS50P outdated Code not rejectecing bad formatted inputs

import re

months = [
    "January",
    "February",
    "March",
    "April",
    "May",
    "June",
    "July",
    "August",
    "September",
    "October",
    "November",
    "December"
]

def main():
    conversion()


def conversion():
    pattern = r'[ /, ]+'
    while True:
        try:
            date = input("Date: ").strip()
            month, day, year = re.split(pattern, date)
            if month in months:
                month = months.index(month) + 1
            month = int(month)
            day = int(day)
            year = int(year)
            #print(day)
            if day > 31 or month > 12:
                pass
            else:
                print(f"{year}-{month:02}-{day:02}")
                break
        except ValueError:
            pass

main()
Source Link
enzo
  • 17
  • 4

CS50P outdated Code not resulting in mixed up date formats

import re

months = [
    "January",
    "February",
    "March",
    "April",
    "May",
    "June",
    "July",
    "August",
    "September",
    "October",
    "November",
    "December"
]

def main():
    conversion()


def conversion():
    pattern = r'[ /, ]+'
    while True:
        try:
            date = input("Date: ").strip()
            month, day, year = re.split(pattern, date)
            if month in months:
                month = months.index(month) + 1
            month = int(month)
            day = int(day)
            year = int(year)
            #print(day)
            if day > 31 or month > 12:
                pass
            else:
                print(f"{year}-{month:02}-{day:02}")
                break
        except ValueError:
            pass

main()

The following two outputs which are about mixed format input like, October/8/1701 and September 8 1636,are not what they should be and i can`t make them work

enter image description here