如何在HackerRank中报错退出前输出所有输入内容?
Fixing Input Error Handling to Print Entered Content Before Exiting
Hey there, let's break down and fix your problem. Right now, your program has two main issues causing the behavior you don't want:
- The end date inputs (month/day/year) aren't wrapped in the
tryblock—so if those fail, they'll crash the program outright instead of hitting your error handler. - There's no way to track and print the inputs the user already entered before the error occurred.
Here's the revised code that addresses both problems, plus adds clearer error messaging:
# Keep track of all valid inputs the user has entered so far entered_inputs = [] try: # Handle start date inputs start_month = int(input('Enter start month: ')) entered_inputs.append(f"Start Month: {start_month}") assert 1 <= start_month <= 12, "Start month must be between 1 and 12" start_day = int(input('Enter start day: ')) entered_inputs.append(f"Start Day: {start_day}") assert 1 <= start_day <= 31, "Start day must be between 1 and 31" start_year = int(input('Enter start year: ')) entered_inputs.append(f"Start Year: {start_year}") if start_month == 2 and start_day == 29: assert (1971 <= start_year <= 2020) and (start_year % 400 == 0 or (start_year % 4 == 0 and start_year % 100 != 0)), "Start year isn't a valid leap year (1971-2020)" else: assert 1971 <= start_year <= 2020, "Start year must be between 1971 and 2020" # Now handle end date inputs (moved inside the try block!) end_month = int(input('Enter end month: ')) entered_inputs.append(f"End Month: {end_month}") assert 1 <= end_month <= 12, "End month must be between 1 and 12" end_day = int(input('Enter end day: ')) entered_inputs.append(f"End Day: {end_day}") assert 1 <= end_day <= 31, "End day must be between 1 and 31" end_year = int(input('Enter end year: ')) entered_inputs.append(f"End Year: {end_year}") if end_month == 2 and end_day == 29: assert (start_year <= end_year <= 2020) and (end_year % 400 == 0 or (end_year % 4 == 0 and end_year % 100 != 0)), "End year isn't a valid leap year (>= start year, <=2020)" else: assert start_year <= end_year <= 2020, "End year must be between start year and 2020" # Catch specific errors instead of a broad except (better practice!) except (ValueError, AssertionError) as error: # First, print everything the user already entered print("\nYour entered inputs:") for item in entered_inputs: print(f"- {item}") # Then show the error details and exit print("\nInvalid Input:", str(error) if str(error) else "Unknown issue occurred") print("Exiting Program.") exit() # If we get here, all inputs are valid—proceed with your main logic print("\nAll inputs are valid! Continuing program...") # Add your date calculation or other logic here
Key Changes I Made:
- Tracked entered inputs: Added a
entered_inputslist that logs every valid input as it's entered. This lets us print them out if an error happens later. - Wrapped all inputs in try: Moved the end date inputs inside the
tryblock so any errors there are caught and handled properly. - Specific error catching: Instead of a generic
except, we catchValueError(for non-integer inputs) andAssertionError(for invalid range/leap year checks) to avoid hiding unexpected bugs. - Clearer error messages: Added custom messages to each
assertso users know exactly what went wrong (e.g., "Start year isn't a valid leap year" instead of a blank assertion error).
Now when an error occurs, the program will first print out all the inputs the user successfully entered, then explain the error—exactly what you needed for HackerRank submission.
内容的提问来源于stack exchange,提问作者Jerxe
相关产品推荐
相关产品推荐

