python - "How to think like a computer scientist 3"练习没有值(value)

标签 python nonetype pyscripter

我正在通过一本很棒的书“如何像计算机科学家一样思考 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_semanavacaciones函数而不打印它,否则更改 print dias_semana 中的语句至 return声明。

关于python - "How to think like a computer scientist 3"练习没有值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40293137/

相关文章:

python 类型错误: 'NoneType' object has no attribute '__getitem__'

python - 从列表中删除 NoneType 元素的 native Python 函数?

python - TypeError : float() argument must be a string or a number, 不是 'NoneType'

Python:如何在屏幕上输入密码时隐藏密码 "asterisk out"?

python - Cygwin 上的 Pyscripter

python - 我如何遍历一年中的几个月并在 python 中打印它们?

python - 更新最新 PySide 后 Pyinstaller 错误

python - 如何计算多类图像分割的多类骰子系数?

Python 'list' 对象不可调用

python - python 中的扩展切片?