首页 > 解决方案 > == 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”会对代码的运行方式产生影响?

标签: python

解决方案


== 0表示两个数的余数为零。

x % y给出 的余数,如果没有余数(即余数为 0)x / y,您可以说均匀y进入。x但是,x % y不会总是等于零。例如3 % 2给出 1。因此== 0检查由count % 4和计算的余数count % 6是否为零(无余数)


推荐阅读