Python 创建一个函数,检查一个数字n是否可被两个数字x和y整除。所有输入均为正非零数字

Python 创建一个函数,检查一个数字n是否可被两个数字x和y整除。所有输入均为正非零数字,python,Python,为什么要使用elif?“可被两个数中的每一个数整除”的反义词不是“不能被任何一个数整除”,而是“不能同时被两个数整除”。简单的else即可: def is_divisible(n,x,y): #your code here n = int(input()) x = int(input()) y = int(input()) if n % x == 0 and y % x == 0: return True elif n %

为什么要使用
elif
?“可被两个数中的每一个数整除”的反义词不是“不能被任何一个数整除”,而是“不能同时被两个数整除”。简单的
else
即可:

def is_divisible(n,x,y):
    #your code here
    n = int(input())
    x = int(input())
    y = int(input())
    if n % x == 0 and y % x == 0:
            return True
    elif n % x != 0 and y % x != 0:
        return False
       
    
result = is_divisible(n=int(input()),x=int(input()),y=int(input()))
print(result)
简言之:

def is_divisible(n, x, y):
    if n % x == 0 and n % y == 0:  # use n both times!
        return True
    else:  # this case is: n % x != 0 OR n % y != 0
        return False

还请注意,您已经将用户输入传递给函数。无需再次执行。

为什么要为函数提供n、x、y,然后再次在函数中输入?这本质上是一个
返回n%x==0和n%y==0
def is_divisible(n, x, y):
    return n % x == n % y == 0