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!")
Related
how should the following code be structured in order for the correct statements to be printed when the user has input an integer?
What am i doing wrong? i have tried to change the code in so many ways with no luck.
x = int(input("Please enter a number:\n"))
if x % 3 == 0 and x % 5 == 0:
print("Your number is divisible by 3 and 5.")
if x % 3 == 0 and x % 5 == 1:
print("Your number is divisible by 3 and NOT 5.")
elif x % 3 == 1 and x % 5 == 0:
print("Your number is NOT divisible by 3 and is divisible by 5.")
else:
print("Your number is NOT divisible by 3 and 5.")
or
x = int(input("Please enter a number:\n"))
if ((x % 3 == 0) & (x % 5 == 0)):
print("Your number is divisible by 3 and 5.")
if ((x % 3 == 0) & (x % 5 == 1)):
print("Your number is divisible by 3 and NOT 5.")
elif ((x % 3 == 1) & (x % 5 == 0)):
print("Your number is NOT divisible by 3 and is divisible by 5.")
else:
print("Your number is NOT divisible by 3 and 5.")
I want the correct phrase to be displayed once the user has input their chosen integer.
Each time you think you're checking for "not divisible by a number", you're actually checking "exactly one greater than a multiple of that number". The test shouldn't be == 1, it should be != 0 to cover when the remainder is anything but zero (a number that is not divisible by 3 might have remainders of 1 or 2, a number not divisible by 5 might have remainders of 1, 2, 3 or 4; you just care if the remainder is 0 or "something else", not 1 specifically).
You also over-indented two of your tests (so they'd only get tested when x % 3 == 0 and x % 5 == 0, and the tests were guaranteed to fail). Fixed code:
x = int(input("Please enter a number:\n"))
if x % 3 == 0 and x % 5 == 0:
print("Your number is divisible by 3 and 5.")
elif x % 3 == 0 and x % 5 != 0: # Changed % 5 test, dedented and made elif
print("Your number is divisible by 3 and NOT 5.")
elif x % 3 != 0 and x % 5 == 0: # Changed % 3 test and dedented
print("Your number is NOT divisible by 3 and is divisible by 5.")
else:
print("Your number is NOT divisible by 3 and 5.")
It's possible to simplify further (so you don't recompute x % 3 and x % 5 up to three times each), and Byron's answer provides a good example of that, but I left this as close to your original code as possible to demonstrate the minimal fixes (though I did PEP8-ify your indentation levels to make them easier to read: The One True Indentation is four spaces per level).
Awful shorthand but here:
if x % 3 == 0:
if x % 5 == 0:
print("Your number is divisible by 3 & 5")
else:
print("Your number is divisible by 3 and NOT 5.")
elif x % 5 == 0:
print("Your number is NOT divisible by 3 and is divisible by 5.")
else:
print("Your number is NOT divisible by 3 and 5.")
Also, your check where you do "x % 3 == 1" doesn't make a whole lot of sense because x % 3 could also be 2, in which case the branch wouldn't get hit.
b = input().split(' ')
c = list(map(int,b))
y = 0
for i in range(len(b)):
if c[i] %2 == 0:
print(c[i])
y = 1
elif i == len(c) - 1 & y == 0:
print('No number that is divisible by 2')
Code takes a list of numbers as input, then it prints all values divisible by 2, but if there are no such values it should print about this only once. My code works properly only in certain cases. I know another implementation but i need solution within 1 loop
Add break:
b = input().split(' ')
c = list(map(int,b))
y = 0
for i in range(len(b)):
if c[i] %2 == 0:
print(c[i])
y = 1
elif i == len(c) - 1 & y == 0:
print('No number that is divisible by 2')
break
You may want to simplify by calculating the numbers divisible by 2 first, then deciding what to print:
nums = map(int, input().split(' '))
div2 = [i for i in nums if not i % 2]
if div2:
for i in div2:
print(i)
else:
print('No number that is divisible by 2')
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 am doing a challenge but for some reason every time i run it says 3 outta of 7 test cases are incorrect and don't know why? everything seems in order. Here is the challenge if Task
Given an integer, , perform the following conditional actions:
If is odd, print Weird
If is even and in the inclusive 2 range of 5 to , print Not Weird
If is even and in the inclusive range of 6 to 20, print Weird
If is even and greater than 20, print Not Weird
My code below:
n = int(input().strip())
if n % 2 != 0:
print("Weird")
else:
if n % 2 == 1 and n in range(2,5):
print("Not Weird")
elif n % 2 == 1 and n in range(6,20):
print("Weird")
elif n > 20:
print("Not Weird")
Try this
n = int(input().strip())
if n % 2 != 0:
print("Weird")
else:
if n in range(2,6):
print("Not Weird")
elif n in range(6,21):
print("Weird")
elif n > 20:
print("Not Weird"
To include 5 and 20 in range you need to specify it as number + 1. Range does not include the last number. Also, there is no need to check for even condition every time in the else part as the control jumps to else when if fails!.
n = int(input().strip())
if n % 2 != 0:
print("Weird")
else:
if n in range(2,6):
print("Not Weird")
elif n in range(6,21):
print("Weird")
elif n > 20:
print("Not Weird")
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)