1

had just joined the community and picked up python3 quite recently. Am currently learning through the resource 'How To Think Like A Computer Scientist With Python3, 3rd Edition'. I was working through some exercises on chapter 4: functions and came across this problem: Problem in question

Currently my attempt at the problem is this(please forgive the formatting):

print('-----------Ex 2-----------')

days_of_week = ('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday')


def day_name(n):

    if n >= 0 and n < 7:
        return str(days_of_week[int(n)])
    else:
        return 'None'


print(day_name(1))

print('-----------Ex 3-----------')


def num_day(day_name):

    for day in days_of_week:
        if day == day_name:
            return(days_of_week.index(day_name))
        else:
            return 'None'



print(num_day("Tuesday"))

For exercise 2 everything seems to be working fine, however with exercise 3 the function returns the index of the day together with 'None' as in here:Result

Greatly appreciate any explanation on why this occurs and how to remove the presence of the 'None'

Thanks!

kwekie
  • 35
  • 4
  • In Ex 3, the method does not return anything. Hence None. Instead of print(), return the value. – menaka_ Jan 20 '20 at 09:41
  • 1
    besides the explanations given below on what's going on, you could simplify your function `def num_day(day_name):` body to the 1-liner `return days_of_week.index(day_name) if day_name in days_of_week else None`. That makes use of a ternary expression, see e.g. [here](https://stackoverflow.com/a/394814/10197418). – MrFuppes Jan 20 '20 at 12:56

2 Answers2

1

In Python, a function always returns None, if you don't specify any return value. So instead of

print(days_of_week.index(day_name))

You could do

return days_of_week.index(day_name)

So the actual value you want is returned by your function.

DerHamm
  • 161
  • 6
  • it should be taken into account that `return days_of_week.index(day_name)` will raise a `ValueError: tuple.index(x): x not in tuple` if `day_name` is not in `days_of_week` – MrFuppes Jan 20 '20 at 09:51
0

What is happening when you call print(num_day("Tuesday")) is that the for loop iterates through the days of the week. So on the first step of the loop, the variable day is "Monday". Then the if statement is evaluated to false since 'Monday' != 'Tuesday', and so the else block is executed, and the function returns none, which is not what we wanted. Rather we only want to return none after it has iterated through all the week days. So that means we just need to remove the else statement, and return None only if the input did not match any of the week day names.

def num_day(day_name):
    days_of_week = ('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday')
    for day in days_of_week:
        if day == day_name:
            return(days_of_week.index(day_name))
    return 'None'

print(num_day("Tuesday"))

Also its better to define the days_of_week variable inside the function.

rich
  • 376
  • 3
  • 15
  • Ah I see, your explanation definitely helped clarify a few misconceptions. Thanks for sharing your insights! – kwekie Jan 21 '20 at 01:06