python - 需要帮助来理解 python 中的代码

标签 python

first = int(input('first int: '))
second = int (input('second int: '))
result =0
if first and second:
result =1
elif not first:
    result =2
elif first or second:
    result=3
else:
    result=4
print(result)

当我输入 1 和 0 时,结果是 3。如果有人可以添加一些解释,我将不胜感激。

最佳答案

您使用的是or - 这意味着该语句在第一次找到True时将返回True。 当您说 5 或 9 时,5 和 9 都代表真理(任何非零值也是如此)。所以在这种情况下它返回第一个 - 5。当您输入 9 或 5 时,它会返回 9。

编辑:k = 1 或 0 的计算结果为 True,因为 1 代表真理。因此,根据您的代码,结果是3

关于python - 需要帮助来理解 python 中的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21670749/

相关文章:

python - 从内置类型派生时如何设置 "the"值?

python - 如何在不丢失离散值的情况下缩小图像?

javascript - Django 测试用例 - 如何从 .html 调用 javascript 函数进行测试?

python - 无法将当前 flask 请求上下文复制到多个 gevent 线程

python - 在 ipython 中运行任何命令之前,如何让文件自动运行?

python - 智能获取两个字符之间的文本

python - App Engine - 从云存储下载文件

python - 为什么验证形式不起作用?

python - 在 web.py 中呈现 HTML

python - sqlalchemy session 执行搞乱了有效的 postgres 语法