PL/Python

HackerRank Python If-Else

junmosdata 2022. 8. 5. 01:26

Quiz. Given an integer, n, perform the following conditional actions:

  • If n is odd, print Weird
  • If n is even and in the inclusive range of 2 to 5, print Not Weird
  • If n is even and in the inclusive range of 6 to 20, print Weird
  • If n is even and greater than 20, print Not Weird

input. A single line containing a positive integer, n.

output. Print Weird if the number is weird. Otherwise, print Not Weird.

ex)

input

3

output

Weird

 

일단 홀수라면 전부 Weird를 출력해야 하는 코드.

하지만, 짝수라고 전부 Not Weird가 아니라서 부가적인 조건문이 필요하다.

문제의 조건이 복잡해 보이지만 들여다보면,

일단 문제의 세번째 줄에 6~20의 숫자는 짝수라고 하더라도 Weird를 출력해야 한다.

나머지는 모두 Not Weird여도 되므로 결국,

'홀수와 6에서 20의 짝수'는 Weird,

'그 외의 짝수'는 Not Weird를 출력하면 된다.

 

#!/bin/python3

import math
import os
import random
import re
import sys



if __name__ == '__main__':
    n = int(input().strip())
    if n % 2 == 1:
        print('Weird')
    elif 6 <= n <= 20:
        print('Weird')
    else:
        print('Not Weird')