#当你接到下面这道题
当你看到这个题。你会想我就用整除的公式作为判断的条件就可以了。
num // 5 == 0 and num //6 == 0
num // 5 == 0 or num // 6 == 0
(num // 5 == 0 or num // 6 == 0) and not (num // 5 == 0 and num //6 == 0)
可是结果呢却南辕北辙。我用了函数的方式做了几个定义
def num_judge1(num):
if num // 5 == 0 and num //6 == 0:
return True
else:
return False
def num_judge2(num):
if num // 5 == 0 or num //6 == 0:
return True
else:
return False
def num_judge3(num):
if (num // 5 == 0 or num //6 == 0) and not (num // 5 == 0 and num //6 == 0):
return True
else:
return False
但结果却不对。我就不显示结果了。你们自己试一下,现在换一种想法。是整除。可是10、30。整除后是2 、5、6 而不是0。所以直接用整除公式来运算确实得不到我们想要的东西。
最后我重新修改了一下
def num_judge1(num):
if num % 5 == 0 and num % 6 == 0:
return True
else:
return False
def num_judge2(num):
if num % 5 == 0 or num % 6 == 0:
return True
else:
return False
def num_judge3(num):
if (num % 5 == 0 or num % 6 == 0) and not (num % 5 == 0 and num % 6 == 0):
return True
else:
return False
# user_input = eval(input("Enter an integer: "))
user_input = 30 #自己提前输入好的
print(f"Is {user_input} divisible by 5 and 6?", num_judge1(user_input))
print(f"Is {user_input} divisible by 5 or 6?", num_judge2(user_input))
print(f"Is {user_input} divisible by 5 or 6, and not both?", num_judge3(user_input))
结果如下:
分享结束