To check whether a number is multiple of second number

Question:

I want to check whether a number is multiple of second. What’s wrong with the following code?

def is_multiple(x,y):
    if x!=0 & (y%x)==0 :
       print("true")
    else:
       print("false")
    end
print("A program in python")
x=input("enter a number :")
y=input("enter its multiple :")
is_multiple(x,y)

error:

TypeError: not all arguments converted during string formatting
Asked By: Vikranth Inti

||

Answers:

You are using the binary AND operator &; you want the boolean AND operator here, and:

x and (y % x) == 0

Next, you want to get your inputs converted to integers:

x = int(input("enter a number :"))
y = int(input("enter its multiple :"))

You’ll get a NameError for that end expression on a line, drop that altogether, Python doesn’t need those.

You can test for just x; in a boolean context such as an if statement, a number is considered to be false if 0:

if x and y % x == 0:

Your function is_multiple() should probably just return a boolean; leave printing to the part of the program doing all the other input/output:

def is_multiple(x, y):
    return x and (y % x) == 0

print("A program in python")
x = int(input("enter a number :"))
y = int(input("enter its multiple :"))
if is_multiple(x, y):
    print("true")
else:
    print("false")

That last part could simplified if using a conditional expression:

print("A program in python")
x = int(input("enter a number :"))
y = int(input("enter its multiple :"))
print("true" if is_multiple(x, y) else "false")
Answered By: Martijn Pieters

Use and operator instead of bitwise & operator.

You need to conver values to integers using int()

def is_multiple(x,y):
    if x!=0 and (y%x)==0 :
       print("true")
    else:
       print("false")

print("A program in python")
x = int(input("enter a number :"))
y = int(input("enter its multiple :"))
is_multiple(x,y)
Answered By: sinhayash

Some things to mention:

  1. Conditions with and, not & (binary operator)
  2. Convert input to numbers (for example using int()) – you might also want to catch if something other than a number is entered

This should work:

def is_multiple(x,y):
    if x != 0 and y%x == 0:
        print("true")
    else:
        print("false")

print("A program in python")
x = int(input("enter a number :"))
y = int(input("enter its multiple :"))
is_multiple(x, y)
Answered By: adrianus

I tried this and worked also for when x and/or y are equal to 0. Idk if there’s a shorter way of writing it.

Tested with (4,12), (12, 4), (2,0), (0,2), (0, 0)
(result should be : False True False True True).

def exo1(x,y):
    #x = int(input("input number x: "))
    #y = int(input("input number y: "))
    if x==0 and y==0:
        return True 
    if x>0 and y==0:
        return False 
    if y>0 and x==0:
        return True 
    if x!=0 and y!=0 and (x%y)==0: 
        return True
    else:
        return False
print(exo1())
print(exo1(4,12))
print(exo1(12,4))
print(exo1(2,0))
print(exo1(0,2))
print(exo1(0,0))
Answered By: Axq
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.