0

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

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()
3
  • check50 fails because October/9/1701 is not a valid input format. Valid formats are October 9, 1701 and 10/9/1701. Slashes can only be used when all input is numeric values. When you enter a month name, you need the 'comma' format. September 8 1636 is invalid because it is missing the comma.
    – kcw78
    Commented Aug 9 at 18:18
  • I understood that, but I am having problems trying to apply lines that make the code recognize this two types of invalid formats for then asking for a new input
    – enzo
    Commented Aug 13 at 14:40
  • You need to test for 2 different formats: A) TWO slashes or B) ONE comma, THEN apply proper conversion procedure. Other tests to consider: C) day and month are >0, and D) month name is in the list (personally, I prefer a dictionary). All other input should be re-prompted. I posted an "answer" with some psuedo code that might help your thought process.
    – kcw78
    Commented Aug 13 at 16:11

2 Answers 2

1

Here is some pseudo-code to get you started. It outlines the basic logic, but you have to add the code to implement that logic.

while True:
    date = input('Enter Date: ')

    if [your test to check if date has 2 '/'s]:
        ## code to get month, day, year values from slash format
        ## additional tests for valid slash input 
        ## with messages to reflect input error(s)
    elif [your test to check if date has 1 ',']:
        ## code to get month, day, year values from comma format
        ## additional tests for valid comma input 
        ## with messages to reflect input error(s)
    else:
        print(f'Invalid date format entered: {in_date}')

## Once you have correct input format and values, convert and print
3
  • a good more explicit statement of what i was suggesting. i was trying to make him think about it more then spelling it out this verbosely but good job introducing the logical branch based on seperator tokens and then also mentioning further input tests. if anyone upvotes mine they should upvote yours as well. I will upvote yours. Note that you can do all of your input tests in exactly two regex comparisons.
    – UpAndAdam
    Commented Aug 14 at 15:20
  • Thanks, yeah it's a balance to share code without giving away the answer. I tried to thread the needle -- the if/elif/else block outlines the thought process without solving the problem. :-) (And, I just realized I left a line I intended to delete.) A quick note on regex: This is PSet 3, and Regular Expressions aren't covered (in depth) until Lecture 7. So, most students will probably use string manipulation methods.
    – kcw78
    Commented Aug 14 at 16:22
  • thanks for that tip. i only went there since he was using a regex (rather poorly).. i'm kind of surpried they dont teach reg ex for this assignment though. but it is still doable. in roughly the fasion you are doing.
    – UpAndAdam
    Commented Aug 14 at 17:55
0

UPDATED

The problem is that you are never checking strictly for the proper input format and rejecting the user if the input is bad. There are holes in your logic to detect a format mismatch. Namely a glaring hole if they mix the two formats

You currently use one regular expression to see that the text ROUGHLY follows the patterns, however it is not strict enough. You know the exact patterns that are allowed, and there are two of them, I'd suggest checking them both. There are other differences between the two formats besides placement of delimiters that you are completely ignoring. More specifically your regex split is not validating the formats, it's only splitting your components. It's dandy as a splitter and a nice improvement to alternatives I've seen used. But it is not validating other things.

HINT A hint is that you can do the check for validation in exactly 1 or 2 regex match checks. (1 is a bit more advanced, the 2 case is very straight forward and basically combines what kcw78 outlines in his checking logic) If you search for other answers, I have provided for this exact question you can find answers, but I'd suggest figuring it out yourself and asking questions.

I also suggest you break apart your code. conversion should just do conversion, nothing more and nothing less. If you are going to use functions use them, otherwise you might as well dump this code all in main.

Hints:

  • using a while True loop generally indicates you are wrong and don't know what you are doing in this course

  • conversion shouldn't be prompting the user for input; that should happen in main or in another dedicated function which obtains and validates input

  • conversion should have validated input passed into it,

    • It should probably return output instead of printing it itself
  • What happens if the month they enter is textual garbage, like if they enter "Enzouary"?

new

  • What happens if they enter "September/08/2024"? or "12 1, 2024" Both should be rejected. But your reg-ex is not rejecting them.

  • What happens if they enter "September 08 2024" or "09 08 2024" or "september, 8 2024" or "september,8,2024" or "september 8/2024"

  • what about "08/sep1/oo2024oo"

  • what about "#$/afd1@/20$d" ?

  • Try using some bad inputs and ask yourself did the program do what it was supposed to do for these bad inputs?

"They are not what they should be" is not accurate now was your original problem statement. It's not about 'mixed formats' it is about incorrect formats, the input hasn't been properly validated per the instructions.

You can't make them work? What have you tried doing?

10
  • I'm supposed to reprompt the user. I do know what is the error but don't know how to check for this two types of invalid inputs. Thanks about the tip regarding the main function. The while True is how professor told to do. Thanks about the response
    – enzo
    Commented Aug 13 at 14:50
  • updated my answer. checked the spec, i had misread the output. removed bit about reprompting. the issue is you werent in some cases in which you needed to. honed in more about the issue you are encountering. again though your focus is kind of off. It's not about 'these two cases types of input" you are getting wrong. There are infintely many cases you could be getting wrong beyond and within these two. What matters is checking to make sure 'only the right data' can come through and you don't do any real checking at all.
    – UpAndAdam
    Commented Aug 13 at 16:20
  • Question about your while True comment. I used that to re-prompt the user when the input is an invalid date format/data. Can you explain how you did that differently (w/out giving away the solution)?
    – kcw78
    Commented Aug 14 at 17:21
  • as a code practice I don't use while True if i can avoid it... its just a recipe for bad things in larger code bodies usually. i try not to make it a go to. it has its time and placee. As for a hint? it it wasnt a while true loop youd need a condition that was true until you needed to exit... what could you do to have such a condition?
    – UpAndAdam
    Commented Aug 14 at 18:03
  • I only use while true if something is literally intended to be an infinite loop, where exit is not just an ODD case but an extremely exceptional one. this is close-ish but i think it avoids teaching you the proper way to use while loops, because i rampantly see every new person here default to while true for everything. and with bugs it easily leads to infinite loops. the fact that you don't know how to avoid using it is to me literally what tells me you shouldn't be using it. as opposed to saying "not using it would be complicated and cause x and y or be slow"
    – UpAndAdam
    Commented Aug 14 at 18:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .