我正在通过一本很棒的书“如何像计算机科学家一样思考 3”学习 python 3.4 编程。我对我对练习 5.14.2 的回答有些怀疑,它是这样写的:
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.
我尽量不在本周的下一页中使用任何涉及概念的编码技术,我的回应是:
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()
程序运行,并打印出正确的值。但是,它还会在返回日期后打印一个“无”值。
为什么会这样?我不明白为什么它打印“无”。
最佳答案
您正在 dias_semana
内打印,然后打印它返回的内容——但你永远不会返回任何东西。不明确返回任何内容的函数的默认返回值为 None
.
要么直接调用dias_semana
在vacaciones
函数而不打印它,否则更改 print
dias_semana
中的语句至 return
声明。
关于python - "How to think like a computer scientist 3"练习没有值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40293137/