0

I am working on an exercise from the Book "How to Think like a Computer Scientist: Learning Python 3" My code returns the right results. However, when I run the test_suite function, it shows that the tests failed. I have tried debugging and found out that for some reason, the boolean lines eg. day_add("Monday", 4) == "Friday" returns false even though it should be true. How can I correct this? I am attaching my code for reference.

import sys


def day_name(day):
    if day == 1:
        return "Monday"
    elif day == 2:
        return "Tuesday"
    elif day == 3:
        return "Wednesday"
    elif day == 4:
        return "Thursday"
    elif day == 5:
        return "Friday"
    elif day == 6:
        return "Saturday"
    elif day == 0:
        return "Sunday"
    else:
        return "None"


def day_num(day):
    if day == "Sunday":
        return 0
    elif day == "Monday":
        return 1
    elif day == "Tuesday":
        return 2
    elif day == "Wednesday":
        return 3
    elif day == "Thursday":
        return 4
    elif day == "Friday":
        return 5
    elif day == "Saturday":
        return 6
    else:
        return "None"


def day_add(name, delta):
    day = day_num(name)
    end_date = day + delta
    end_date_name = day_name(end_date % 7)
    print(end_date_name)


def test(did_pass):
    """ Print the result of a test."""
    linenum = sys._getframe(1).f_lineno  # Get the caller's line number.
    if did_pass:
        msg = "Test at line {0} ok".format(linenum)
    else:
        msg = ("None".format(linenum))
    print(msg)


def test_suite():
    """ Run the suite of tests for code in this module (this file).
    """
    test(day_add("Monday", 4) == "Friday")
    test(day_add("Tuesday", 0) == "Tuesday")
    test(day_add("Tuesday", 14) == "Tuesday")
    test(day_add("Sunday", 100) == "Tuesday")
    test(day_add("Sunday", 100) == "moon")


test_suite()
DeepSpace
  • 78,697
  • 11
  • 109
  • 154
laurene
  • 9
  • 1

3 Answers3

1

Your day_add function doesn't return anything.

Paul Mitchell
  • 3,241
  • 1
  • 19
  • 22
0

It looks like day_add doesn't actually return the day name, rather just prints it. Try switching it to return end_date_name.

draeceo
  • 105
  • 1
  • 4
0

day_add() is printing instead of returning, so referencing to a variable is not setting anything to it:

def day_add(name, delta):
    day = day_num(name)
    end_date = day + delta
    end_date_name = day_name(end_date % 7)
    return end_date_name
Wasif
  • 14,755
  • 3
  • 14
  • 34