-4
month = 

    
    {"January" : "01",
    "February" : "02" ,
    "March" : "03",
    "April" : "04",
    "May" : "05",
    "June" : "06",
    "July" : "07",
    "August" : "08",
    "September" : "09",
    "October" : "10",
    "November" : "11",
    "December" : "12"
                    }
while True:

    try:
        date = input("Date: ")
        if date[0].isdecimal():
            x, y, z = date.split("/")
            while y < 31:
                print(f"{z}-{x:0>2}-{y:0>2}")
        elif date[0].isalpha():
            x, y, z = date.split(" ")
            y = y.replace(",", "")
            x = x.title()
            while y < 31:
                if x in month:
                    print(f"{z}-{month.value(x)}-{y}")
    except:
        pass

This is my code and I can't stop it, even I use Ctrl+C. What can I try next?

This is the ps3 in Cs50P.

5
  • 2
    Ctrl-C throws a KeyboradInteruptError which you promptly ignore... Commented Jul 7, 2024 at 23:53
  • 2
    Don't catch all exceptions but only specific ones you expect and know how to handle. Commented Jul 7, 2024 at 23:56
  • 1
    In trying out your code and doing some simple viewing of the code, the first thing I noted is that your variables "x", "y", and "z" are still strings, and you are trying to compare a string to an integer. My guess is that you would want to store those values in integer variables. With that you probably should seek out some additional python tutorials, either online or a book. Commented Jul 8, 2024 at 0:03
  • 1
    There should be a way to kill the program anyway (in spite of your except bug ... see the other comments / answers), but will depend on what OS you are running. (And that's not a StackOverflow question. It is a basic "how do I use my computer" question.) Commented Jul 8, 2024 at 0:05
  • 1
    No reason for the bare try/except at all. It just hides errors. Catch the explicit errors you expect, and only when you understand why you want to hide them in the first place. Commented Jul 8, 2024 at 0:11

1 Answer 1

4

Do except Exception: instead of just except: - then you'll be able to stop the program with Ctrl+C.

Sign up to request clarification or add additional context in comments.

2 Comments

And don’t do this either. Figure out which specific exception(s) you’re worried about, then only catch that one.
Thanks so much. I'm going to do it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.