I'm trying to print all numbers in range 1-100 that are divisible by x and y (ie. 2 nad 3). For now i have
for x in range(0, 101):
if x % (2 and 3) == 0: print("2, 3: ", x)
elif x % 2 == 0: print("2: ", x)
elif x % 3 == 0: print("3: ", x)
But it is not accurate, any suggestions?
(2 and 3) evaluates to 3, that's why you never see the condition elif x % 3 == 0 being executed, notice there's no print("3: ", x) in the output of your code, because it'd already fallen into the condition if x % (2 and 3) == 0.
You should be better using if ((x % 2) == 0 and (x % 3) == 0) : print("2, 3: ", x) on that line.
The reason it is not accurate is by writing x % (2 and 3) python is interpreting (2 and 3).(https://docs.python.org/2/reference/expressions.html)
in python (2 and 3) would return 3 because both values are "truthy" and the AND comparison operator in python will return the last value when both items are True.
As per Rajesh Kumar's suggestion you could do
if x % 6 == 0: # ...
or
if x % 2 == 0 and x % 3 == 0: # More verbose...
If you have a number that hast to be dividable by number x and number y you can look at it like:
If some rest is left after a division with divisor x or divisor y, the currently considered number toDivide is not the number you are looking for, because you want numbers where neither of the devisions results in a rest.
x = 2
y = 3
for toDivide in range(1, 101):
# can't divide by x and y
if toDivide%x and toDivide%y:
continue
print((str(x)+", "+str(y) if not toDivide%x and not toDivide%y else (str(x) if not toDivide%x else str(y)))+":"+str(toDivide))
edit: found and resolved the code-error
In if x % (2 and 3) == 0 the value of (2 and 3) is evaluated first, you should first check divisibility by 2 then by 3. i.e.
if (x % 2) and (x % 3)
the two expressions in brackets return booleans that you finally evaluate using and.
corrected:
for x in range(0, 101):
if (x % 2) and (x % 3):
print("2, 3: ", x)
elif x % 2 == 0:
print("2: ", x)
elif x % 3 == 0:
print("3: ", x)
Related
I have been trying to make a simple code with python in which it takes an input 'n' and outputs the numbers from 1 to n
for each multiple of 3, print Solo instead of the number
for each multiple of 5, print Learn instead of the number
for numbers which are multiples of both 3 and 5, output SoloLearn
I want to code it in a way that the logic will skip the even numbers and can only be applied to odd numbers in the range
this is my code below
n = int(input())
for x in range(1, n):
if x % 3 == 0 and x % 5 == 0:
print("SoloLearn")
elif x % 3 == 0:
print("Solo")
elif x % 5 == 0:
print("Learn")
else:
print(x)
Add a continue clause at the top of your loop:
if x % 2 == 0:
continue
For more details, read the docs.
The simplest/fastest/smallest amount of changes you need to make is to just alter the range function to give you exactly what you want.
n = int(input())
for x in range(1, n + 1 , 2):
if x % 3 == 0 and x % 5 == 0:
print("SoloLearn")
elif x % 3 == 0:
print("Solo")
elif x % 5 == 0:
print("Learn")
else:
print(x)
I added n+1 not to satisfy your question, but because I think as written your code was not correct -> for example run these two snippets and see what I mean. If you didn't want n to be part of the range than your code is correct without the n+1.
print("no +1")
for i in range(1,11,2):
print(i)
print("\n\nwith + 1"
for i in range(1,11+1,1):
print(i)
Convert the 'for' loop to 'while' loop . Since we are starting with 1 , adding 2 in each iteration , makes the loop run only for odd numbers.
n = int(input())
x=1
while x<n:
if x % 3 == 0 and x % 5 == 0:
print(x ,"SoloLearn")
elif x % 3 == 0:
print(x, "Solo")
elif x % 5 == 0:
print(x, "Learn")
else:
print(x)
x=x+2
I used a for loop to print the numbers between 0 and 100 that are multiples of three. Now I have to print the ones that are divisible by two and I cant seem to get it to work properly.
I need to show both and I did that below, but is there a way to incorporate the second for loop into the first one in order to print both without printing the divisible ones in the same loop as the multiples of three.
Code:
for x in range(0,100,3):
print(x)
for n in range(0,100,3):
if n % 2 == 0:
print(n)
for x in range (0,100,3):
print("Multi of 3: " + str(x))
if(x % 2 == 0):
print("Div by 2: " + str(x))
if(x % 3 == 0 AND x % 2 == 0 ):
print("Both: " + str(x))
You need to add an if statement to check for numbers which are divisible by 2. If a number is evenly divisible by 2 then the number remainder will be 0. To check this, you can use the % (modulo) operator.
So, you'd have:
for x in range (0, 100, 3):
# check if x is evenly divisible by 2
# i.e. is the remainder zero when divided by 2
if x % 2 == 0:
print(x)
Alternatively, you could go up in steps of 6, since 6 is the LCM of 2 and 3.
for x in range(0, 100, 6):
print(x)
for x in range (0,100,2):
print(x)
This will print the ones divisible by 2 excluding 100
not a expert in python but logicaly all language has a modulo operator
sum = 0
for i in range (yourange):
if (i % 3 == 0)
sum += i
return sum
x=[1,2,3,4,5,6]
for y in x:
if y %2 == 0:
print (y)
elif y %3 == 0:
print ("y")
elif y %3 and y %2 ==0:
print ("Divisible by 2 and 3!")
else:
print ("Odd number!")
I was trying to find out the even and odd numbers between 1 to 6. All worked out fine except the integer 6 where I need it to print out that the integer 6 is divisible by 2 and 3. How can I fix the error?
There are two problems here: first of all, the condition should be:
elif y %3 == 0 and y %2 ==0:
Since there are two separate conditions. Next you need to move that check to the top, so:
for y in x:
if y %3 == 0 and y %2 ==0:
print ("Divisible by 2 and 3!")
elif y %2 == 0:
print (y)
elif y %3 == 0:
print ("y")
else:
print ("Odd number!")
Why? Because if a number is dividable by 2 and 3, it is also dividable by 2. So that means in your case it would have picked the first branch. By swapping the order it will first check if it is dividable by 2 and 3. If that is not the case - that is if at least one condition fails - it will check the remaining branches.
The test for divisible by 2 and 3 should come first, so for 6 you have y %3==0 and y %2 ==0 evaluated instead of y %2 == 0:
for y in x:
if y % 3 == 0 and y % 2 == 0:
print ("Divisible by 2 and 3!")
elif y % 2 == 0:
print (y)
elif y % 3 == 0:
print (y)
else:
print ("Odd number!")
I am trying to write a function to determine if a number is prime. I have come up with
the following solution, however inelegant, but cannot figure out how to write it.
I want to do the following: take the number x and divide it by every number that is less than itself. If any solution equals zero, print 'Not prime.' If no solution equals zero, print 'Prime.'
In other words, I want the function to do the following:
x % (x - 1) =
x % (x - 2) =
x % (x - 3) =
x % (x - 4) =
etc...
Here is as far as I have been able to get:
def prime_num(x):
p = x - 1
p = p - 1
L = (x % (p))
while p > 0:
return L
Wikipedia provides one possible primality check in Python
def is_prime(n):
if n <= 3:
return n >= 2
if n % 2 == 0 or n % 3 == 0:
return False
for i in range(5, int(n ** 0.5) + 1, 6):
if n % i == 0 or n % (i + 2) == 0:
return False
return True
This question already has answers here:
How can I break out of multiple loops?
(39 answers)
Closed 9 years ago.
I've seen many different ways to break out of two nested loops at once, but what is the quickest and simplest for my code?
primes = [2]
for a in range(3, 500, 2):
for b in range(2, int(a ** 0.5 + 0.5)):
if a % b != 0:
primes.append(a)
if a % b == 0:
[x for x in primes if x != a]
# double break
Put the loops in a function and use the return keyword:
def func(primes):
for a in range(3, 500, 2):
for b in range(2, int(a ** 0.5 + 0.5)):
if a % b != 0:
primes.append(a)
if a % b == 0:
[x for x in primes if x != a]
return
primes = [2]
func(primes)
This tends to be a good thing when it makes the programmer write modularized code.
Refactor the double loop as a function and use return to break out.
If you don't want to use a function you can use a variable. Here it is flag
primes = [2]
flag = 0
for a in range(3, 500, 2):
for b in range(2, int(a ** 0.5 + 0.5)):
if a % b != 0:
primes.append(a)
if a % b == 0:
[x for x in primes if x != a]
flag = 1
break
if flag:
break