【问题标题】:Why is function not printing anything to console when called? [duplicate]为什么函数在调用时不向控制台打印任何内容? [复制]
【发布时间】:2016-11-24 04:07:24
【问题描述】:

当我调用vote()函数时,编译器不打印输出。当我调用它时,为什么该函数没有向控制台打印任何内容?

这是我的代码:

def vote(vote_one, vote_two, vote_three):
    if (vote_one == vote_two):
        return True
    elif (vote_one == vote_three):
        return True
    elif (vote_two == vote_three):
        return True
    else:
        return False

vote(1, 2, 1)

【问题讨论】:

  • 缩进真的是这样吗?如果是你的函数没有主体
  • 看起来不错,除了缩进不好,你得到什么错误消息?
  • 你的问题说“没有输入”,那个错误说“没有输出”。
  • 试试print(vote(1, 2, 1))
  • 它工作正常,第二个条件为真,因此返回真

标签: python function if-statement input


【解决方案1】:

这是因为你实际上并没有输出任何东西。当您调用 vote() 时,会返回一个结果(True 或 False),但您实际上并没有使用该结果。你可以这样做

result = vote(1, 2, 1)
print(result)

print(vote(1, 2, 1))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-06
    • 2021-11-16
    相关资源
    最近更新 更多