In this challenge you are given an integer, n, and asked to perform the following conditional actions:
- If
Nis odd, print Weird - If
Nis even and in the inclusive range of 2 to 5, print Not Weird - If
Nis even and in the inclusive range of 6 to 20, print Weird - If
Nis even and greater than 20, print Not Weird
Constraints: 1 <= N <= 100
Sample Input:
3
Sample Output:
Weird
One solution would be to simply go through the bullet points and make an if statement for each or an if/elif/else statement. Although it’s not the most efficient solution, it will run correctly.
N = int(input())
#If N is odd, print Weird
if N % 2 != 0: #if n is odd
print('Weird')
#If N is even and in the inclusive range of 2 to 5, print Not Weird
elif N % 2 == 0 and N >= 2 and N <= 5:
print('Not Weird')
#If N is even and in the inclusive range of 6 to 20, print Weird
elif N % 2 == 0 and N >= 6 and N <= 20:
print('Weird')
else:
print('Not Weird')
This can be shortened quite a bit. ‘Weird’ is printed when n is odd and when it’s even between 6 and 20. In all other cases, it will print ‘Not Weird’
Sample Solution:
N = int(input())
#If n is odd or between 6 and 20
if N % 2 != 0 or (N >=6 and N <= 20):
print('Weird')
else:
print('Not Weird')
The print function can also condense if/else statements into one line. But the goal is clear and readable code, not necessarily the shortest possible program.
N = int(input())
print('Weird' if (N % 2 != 0 or (6 <= N <= 20)) else 'Not Weird')