![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python program to check a number n is weird or not
Suppose we have a number n. We shall have to check whether n is weird or not. Here a number is weird when − 1. The number is odd 2. The number is not in range 2 to 5 3. The number is even and in range 6 to 20
So, if the input is like n = 18, then the output will be Weird because it is even and in range 6 to 20.
To solve this, we will follow these steps −
- if n is odd, then
- return "Weird"
- otherwise when (n > 1 and n < 6) or n > 20, then
- return "Not Weird"
- otherwise when n > 6 and n < 21, then
- return "Weird"
Example
Let us see the following implementation to get better understanding −
def solve(n): if n & 1: return "Weird" elif (n > 1 and n < 6) or n > 20: return "Not Weird" elif n > 6 and n < 21: return "Weird" n = 18 print(solve(n))
Input
18
Output
Weird
Advertisements