Question
Instructions:
Debug the following program so that it can be executed and run correctly without errors. Start by finding and fixing the 2 syntax errors (so you can get the program to run!), then find and correct the 2 logic errors.
Finally, see if you can improve the program by implementing a try-except statement to handle invalid user-input.
Note - there are no mathematical or logic errors within the calculation or algorithm itself.
# ------ Program Description ------
# This is a sample program that allows user to enter a date in the format mm/dd/yyyy
# then returns which day of the week that date was.
#
def getDayOfWeek(day, month, year)
# Doomsday algorithm for finding day of week for any date
# January and February leap years
if month < 3:
month = month + 12
year = year - 1
# Doomsday Formula
a = year % 100
b = year // 100
result = (day + (13 * (month + 1)) // 5 + a + a // 4 + b // 4 + 5 * b) % 7
# Store days of week: Index 0-6
weekDays = ["Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
return weekDays[result]
# Main Driver Code
running = True
while (running == False):
# Get user input date and parse using '/'
inputVal = input("\nPlease enter a date in the form: mm-dd-yyyy\n\n")
date = inputVal.split("/"
# Parse input into mm, dd, yyyy
month = int(date[0])
day = int(date[1])
year = int(date[2])
print(inputVal + " was a " + getDayOfWeek(day, month, year))
response = input("Enter Q to quit or hit enter to calculate another date.\n")
# Exit Program
if(response == "Q" or response == "q"):
print("Goodbye!")
running = False
Debug the following program so that it can be executed and run correctly without errors. Start by finding and fixing the 2 syntax errors (so you can get the program to run!), then find and correct the 2 logic errors.
Finally, see if you can improve the program by implementing a try-except statement to handle invalid user-input.
Note - there are no mathematical or logic errors within the calculation or algorithm itself.
# ------ Program Description ------
# This is a sample program that allows user to enter a date in the format mm/dd/yyyy
# then returns which day of the week that date was.
#
def getDayOfWeek(day, month, year)
# Doomsday algorithm for finding day of week for any date
# January and February leap years
if month < 3:
month = month + 12
year = year - 1
# Doomsday Formula
a = year % 100
b = year // 100
result = (day + (13 * (month + 1)) // 5 + a + a // 4 + b // 4 + 5 * b) % 7
# Store days of week: Index 0-6
weekDays = ["Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
return weekDays[result]
# Main Driver Code
running = True
while (running == False):
# Get user input date and parse using '/'
inputVal = input("\nPlease enter a date in the form: mm-dd-yyyy\n\n")
date = inputVal.split("/"
# Parse input into mm, dd, yyyy
month = int(date[0])
day = int(date[1])
year = int(date[2])
print(inputVal + " was a " + getDayOfWeek(day, month, year))
response = input("Enter Q to quit or hit enter to calculate another date.\n")
# Exit Program
if(response == "Q" or response == "q"):
print("Goodbye!")
running = False
Answers
GPT-4o mini
Here is the corrected program with all the specified issues (syntax errors and logic errors) fixed, along with the try-except block to handle invalid user input:
```python
# ------ Program Description ------
# This is a sample program that allows the user to enter a date in the format mm/dd/yyyy
# then returns which day of the week that date was.
#
def getDayOfWeek(day, month, year):
# Doomsday algorithm for finding the day of the week for any date
# January and February leap years
if month < 3:
month += 12
year -= 1
# Doomsday Formula
a = year % 100
b = year // 100
result = (day + (13 * (month + 1)) // 5 + a + a // 4 + b // 4 + 5 * b) % 7
# Store days of week: Index 0-6
weekDays = ["Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
return weekDays[result]
# Main Driver Code
running = True
while running: # Fixed logic error: changed False to True to allow the loop to run
try:
# Get user input date and parse using '/'
inputVal = input("\nPlease enter a date in the form: mm/dd/yyyy\n\n")
date = inputVal.split("/") # Fixed syntax error: changed from split("/") to split("/")
# Parse input into mm, dd, yyyy
month = int(date[0])
day = int(date[1])
year = int(date[2])
print(inputVal + " was a " + getDayOfWeek(day, month, year))
response = input("Enter Q to quit or hit enter to calculate another date.\n")
# Exit Program
if response == "Q" or response == "q":
print("Goodbye!")
running = False
except (ValueError, IndexError):
print("Invalid input. Please enter the date in the correct format: mm/dd/yyyy.")
```
### Summary of Changes Made:
1. **Syntax Fixes:**
- Added a colon `:` after the function definition `def getDayOfWeek(day, month, year)`.
- Corrected the split syntax error: replaced `split("/")` (missing closing parenthesis) with the correct version.
2. **Logic Fixes:**
- The `while` condition was changed from `running == False` to `running` to allow the loop to run initially.
- The correct date separator is `/` in both the user prompt and the parsing logic, ensuring consistency.
3. **Error Handling:**
- A `try-except` block was added to catch and handle `ValueError` (e.g., if conversion to `int` fails) and `IndexError` (if the wrong number of parts are provided) to make the program more robust against invalid inputs.
```python
# ------ Program Description ------
# This is a sample program that allows the user to enter a date in the format mm/dd/yyyy
# then returns which day of the week that date was.
#
def getDayOfWeek(day, month, year):
# Doomsday algorithm for finding the day of the week for any date
# January and February leap years
if month < 3:
month += 12
year -= 1
# Doomsday Formula
a = year % 100
b = year // 100
result = (day + (13 * (month + 1)) // 5 + a + a // 4 + b // 4 + 5 * b) % 7
# Store days of week: Index 0-6
weekDays = ["Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
return weekDays[result]
# Main Driver Code
running = True
while running: # Fixed logic error: changed False to True to allow the loop to run
try:
# Get user input date and parse using '/'
inputVal = input("\nPlease enter a date in the form: mm/dd/yyyy\n\n")
date = inputVal.split("/") # Fixed syntax error: changed from split("/") to split("/")
# Parse input into mm, dd, yyyy
month = int(date[0])
day = int(date[1])
year = int(date[2])
print(inputVal + " was a " + getDayOfWeek(day, month, year))
response = input("Enter Q to quit or hit enter to calculate another date.\n")
# Exit Program
if response == "Q" or response == "q":
print("Goodbye!")
running = False
except (ValueError, IndexError):
print("Invalid input. Please enter the date in the correct format: mm/dd/yyyy.")
```
### Summary of Changes Made:
1. **Syntax Fixes:**
- Added a colon `:` after the function definition `def getDayOfWeek(day, month, year)`.
- Corrected the split syntax error: replaced `split("/")` (missing closing parenthesis) with the correct version.
2. **Logic Fixes:**
- The `while` condition was changed from `running == False` to `running` to allow the loop to run initially.
- The correct date separator is `/` in both the user prompt and the parsing logic, ensuring consistency.
3. **Error Handling:**
- A `try-except` block was added to catch and handle `ValueError` (e.g., if conversion to `int` fails) and `IndexError` (if the wrong number of parts are provided) to make the program more robust against invalid inputs.