Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Jamel Stringer
Courses Plus Student 906 PointsMy if and elif statement is now working
The following code should be easy, but it is not working the way I intended. Can someone explain what I am doing wrong please? Thank you.
num = int(input("Enter a number: "))
n = num % 2
if (n != 0):
print('Weird')
if (n == 0 and n == range(2,6)):
print('Not Weird')
if (n == 0 and n == range(6,21)):
print('Weird')
if (n > 20):
print('Not Weird')
[MOD: added ```python formatting -cf]
1 Answer
Chris Freeman
Treehouse Moderator 68,468 PointsAfter running your code, I see the issue. The modulo operation % 2 will return a 0 for even numbers and a 1 for odd numbers. So if num is odd, you get "weird". If num is even, n is 0 and no if condition matches. Perhaps you meant to comparenum` to the range.
However, n == range() will always be false because you cannot compare an integer with and an iterable. Instead, check if the integer is in the range iterable:
#...
if (n == 0 and num in range(2,6)):
#...
Style tip: The parens around the if conditions are not strictly needed and are usually left off to improve readability.
Post back if you need more help. Good luck!!!
Chris Freeman
Treehouse Moderator 68,468 PointsChris Freeman
Treehouse Moderator 68,468 PointsCan you state what unexpected behavior you are seeing?