python - == 0在python中的意义。(初学者)
问题描述
我对编码很陌生,我在 python 中发现了一些让我很困惑的东西。
在这段小代码中:
if (count % 4) == 0 and (count % 6) == 0:
print ("Cheesecrackers")
count = count + 1
elif (count % 4) == 0:
print("Cheese")
count = count + 1
elif (count % 6) == 0:
print ("Crackers")
count = count +1
else:
print (count)
count = count + 1
行: if (count % 4) == 0 and (count % 6) == 0: 为什么添加“==0”会对代码的运行方式产生影响?
解决方案
== 0
表示两个数的余数为零。
x % y
给出 的余数,如果没有余数(即余数为 0)x / y
,您可以说均匀y
进入。x
但是,x % y
不会总是等于零。例如3 % 2
给出 1。因此== 0
检查由count % 4
和计算的余数count % 6
是否为零(无余数)
推荐阅读
- typescript - 在 Angular 中评估枚举
- asp.net-core - .Net Core Swagger 不适用于必需的枚举?
- android - 在 Mac 上使用 Android Studio 在命令行中构建 Android 应用
- android - 尝试在空对象引用上调用虚拟方法 在空对象引用上调用虚拟方法
- ruby-on-rails - Rails 6 ActiveStorage 中的直接上传问题
- windows - 用批处理替换文件名中的字符
- javascript - 通过 Javascript 用 curInnerHTML 替换 HTML 中的电话号码
- javascript - jQuery 在完成之前等待每个迭代完成的两个单独的 AJAX 调用
- json - 字符串形式的 JSON 正文
- mysql - 如何为 mysql 8 docker 容器启用 x 协议