Claudio Villegas
Claudio Villegas

Reputation: 21

None value on "How to think like a computer scientist 3" exercise

I'm learning python 3.4 programming with the great book "How to Think Like a Computer Scientist 3". I have some doubts about my response to exercise 5.14.2, which reads:

You go on a wonderful holiday (perhaps to jail, if you don’t like happy exercises) leaving on day number 3 (a Wednesday). You return home after 137 sleeps. Write a general version of the program which asks for the starting day number, and the length of your stay, and it will tell you the name of day of the week you will return on.

I'm trying to not use any coding techniques involving concepts in the next pages of the week, and my response is:

def dias_semana(x):
    if x == 1:
        print("lunes")
    elif x == 2:
        print("martes")
    elif x == 3:
        print("miercoles")
    elif x == 4:
        print("jueves")
    elif x == 5:
        print("viernes")
    elif x == 6:
        print("sábado")
    elif x == 7:
        print("domingo")
    else:
        print("déjate de wear culiao")

def vacaciones():
    diaempiezo = int(input("Día que empiezo"))
    cuantosdias = int(input("días de vacaciones"))
    díavuelta = (diaempiezo + cuantosdias) % 7
    print(dias_semana(díavuelta))

vacaciones()

The program works, and prints the correct value. However, it also prints a "None" value after the day of return.

Why this is happening? I don't understand why it prints the "None".

Upvotes: 2

Views: 1007

Answers (2)

xZise
xZise

Reputation: 2379

Your function dias_semana doesn't return anything and in that case a function implicitly returns None. And by doing print(dias_semana(díavuelta)) you actually do the following:

return_value = dias_semana(díavuelta)
print(return_value)

And return_value is None. One way to fix is to return the value in dias_semana and not directly print anything. Or to not use the print function in the vacaciones function.

Upvotes: 2

B. Eckles
B. Eckles

Reputation: 1644

You're printing within dias_semana, and then printing what it returns-- but you never return anything. The default return value for a function which doesn't return anything explicitly, is None.

Either just call dias_semana in the vacaciones function without printing it, or else change the print statements inside of dias_semana to return statements.

Upvotes: 4

Related Questions