i have completed the project test with vscode and return exactly what the output expected but when i paste the code & run the test it didn’ t run. Can you help me what’s wrong.
Could you share your code?
you can click the link
It’s not possible for me to access your code that way. You will need to copy it and share it here.
def add_time(start, duration), start_day=None):
#Splitting the start time to hour, minute, PM/AM
splitStart = start.split(); splitDuration = duration.split(":")
splitTime = splitStart[0].split(":"); durationHour = int(splitDuration[0]); durationMinute = int(splitDuration[1])
hour = int(splitTime[0]); minute = int(splitTime[1])
#List of the day
daysName = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"]
#Check if the day is provided from perimeter
if start_day:
start = daysName.index(start_day.lower())
else:
pass
day = 0 #Initialize the day count
sign = None #Initialize the PM/AM
#Condition if the hour more than 12 convert to 24hour
if splitStart[1] == "PM":
hour = hour + 12
#calculate the duration
day = durationHour // 24; durationHourToAdd = durationHour - (day * 24)
added_hour = hour + durationHourToAdd; added_minute = minute + durationMinute
#Check if calculated minute bigger than 60
if added_minute > 60:
added_minute = (added_minute + 00) - 60
added_hour += 1
#Checking all the possible condition for calculating hour
if added_hour > 24 and added_hour > 12:
added_hour -= 24
if added_hour > 12:
sign = "PM"
added_hour -= 12
elif added_hour < 12:
sign = "AM"
day += 1
elif added_hour < 24 and added_hour > 12:
sign = "PM"
if added_hour < 12:
sign = "AM"
added_hour = added_hour - 12
elif added_hour < 24 and added_hour < 12:
sign = "AM"
elif added_hour == 24:
added_hour = 12
sign = "AM"
day += 1
#Calculating the day string
if start_day:
for _ in range(start, day+daysName.index(start_day.lower())):
daysName[start]
start += 1
if start> len(daysName)-1:
start = 0
daysName[start]
#return the possible output
if not start_day and day == 0:
new_time = f"'{added_hour}:{added_minute:02} {sign}'"
elif not start_day and day == 1:
new_time = f"'{added_hour}:{added_minute:02} {sign}'"
elif not start_day and day > 1:
new_time = f"'{added_hour}:{added_minute:02} {sign}, ({day} days later)'"
elif start_day and day == 0:
new_time = f"'{added_hour}:{added_minute:02} {sign}, {daysName[start].capitalize()}'"
elif start_day and day == 1:
new_time = f"'{added_hour}:{added_minute:02} {sign}, {daysName[start].capitalize()} (next day)'"
elif start_day and day > 1:
new_time = f"'{added_hour}:{added_minute:02} {sign}, {daysName[start].capitalize()} ({day} days later)'"
return new_time
Thanks for the update.
There’s couple details, main reason why it might look like nothing is happening is SyntaxError
:
def add_time(start, duration), start_day=None):
^
SyntaxError: unmatched ')'
Another issue is using literal '
around the returned time. They are in the test hints to indicate that string should be returned.
ok i’ve corrected the syntax error & what do u mean by issue with using ’ ?
The f"'(...)'"
, this is adding '
at the start and end of the string.
still can’t run the test. It’s fine in vscode exactly the same output
def add_time(start, duration, start_day=None):
#Splitting the start time to hour, minute, PM/AM
splitStart = start.split(); splitDuration = duration.split(":")
splitTime = splitStart[0].split(":"); durationHour = int(splitDuration[0]); durationMinute = int(splitDuration[1])
hour = int(splitTime[0]); minute = int(splitTime[1])
#List of the day
daysName = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"]
#Check if the day is provided from perimeter
if start_day:
start = daysName.index(start_day.lower())
else:
pass
day = 0 #Initialize the day count
sign = None #Initialize the PM/AM
#Condition if the hour more than 12 convert to 24hour
if splitStart[1] == "PM":
hour = hour + 12
#calculate the duration
day = durationHour // 24; durationHourToAdd = durationHour - (day * 24)
added_hour = hour + durationHourToAdd; added_minute = minute + durationMinute
#Check if calculated minute bigger than 60
if added_minute > 60:
added_minute = (added_minute + 00) - 60
added_hour += 1
#Checking all the possible condition for calculating hour
if added_hour > 24 and added_hour > 12:
added_hour -= 24
if added_hour > 12:
sign = "PM"
added_hour -= 12
elif added_hour < 12:
sign = "AM"
day += 1
elif added_hour < 24 and added_hour > 12:
sign = "PM"
if added_hour < 12:
sign = "AM"
added_hour = added_hour - 12
elif added_hour < 24 and added_hour < 12:
sign = "AM"
elif added_hour == 24:
added_hour = 12
sign = "AM"
day += 1
#Calculating the day string
if start_day:
for _ in range(start, day+daysName.index(start_day.lower())):
daysName[start]
start += 1
if start> len(daysName)-1:
start = 0
daysName[start]
#return the possible output
if not start_day and day == 0:
new_time = print("{}:{:02} {}".format(added_hour, added_minute, sign))
elif not start_day and day == 1:
new_time = print("{}:{:02} {}".format(added_hour, added_minute, sign))
elif not start_day and day > 1:
new_time = print("{}:{:02} {} ({} days later)".format(added_hour, added_minute, sign, day))
elif start_day and day == 0:
new_time = print("{}:{:02} {}, {}".format(added_hour, added_minute, sign, daysName[start].capitalize()))
elif start_day and day == 1:
new_time = print("{}:{:02} {}, {} (next day)".format(added_hour, added_minute, sign, daysName[start].capitalize()))
elif start_day and day > 1:
new_time = print("{}:{:02} {}, {} ({} days later)".format(added_hour, added_minute, sign, daysName[start].capitalize(), day))
return new_time
Updated code prints the result on it own, it is expected that function will return the string. It is fine to use f-strings, but without additional '
at the start and end.
thank for the help but now i get this error
this is the updated code
if not start_day and day == 0:
new_time = f"{added_hour}:{added_minute:02} {sign}"
elif not start_day and day == 1:
new_time = f"{added_hour}:{added_minute:02} {sign}"
elif not start_day and day > 1:
new_time = f"{added_hour}:{added_minute:02} {sign} ({day} days later)"
elif start_day and day == 0:
new_time = f"{added_hour}:{added_minute:02} {sign}, {daysName[start].capitalize()}"
elif start_day and day == 1:
new_time = f"{added_hour}:{added_minute:02} {sign}, {daysName[start].capitalize()} (next day)"
elif start_day and day > 1:
new_time = f"{added_hour}:{added_minute:02} {sign}, {daysName[start].capitalize()} ({day} days later)"
If I add print('output', add_time('3:30 PM', '2:12'))
to test your function, the output is None
To test
Expected time to end with
'(next day)'
when it is the next day.
I have added
print('output', add_time('3:30 PM', '12:12'))
, it outputs 3:42 AM
which is correct, but it also needs to be 3:42 AM (next day)
it doesn’t look like you have the right output in your code:
elif not start_day and day == 1:
new_time = f"{added_hour}:{added_minute:02} {sign}"
thanks for helping me. I think this should be 2:59AM (next day)
You are right! The test works as it should, it’s only an issue with the text, but it will be fixed soon.
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.